/install latex-verbatim-center
LaTeX Verbatim 居中技能
技能说明
技能名称:latex-verbatim-center
适用场景:当 LaTeX 文档中的 verbatim 环境(代码块、ASCII 图表等)无法正确居中时使用
技能标签:#LaTeX #排版 #居中 #verbatim #varwidth
问题描述
在 LaTeX 中,当尝试将 verbatim 环境放在 center 环境中时,内容往往不会真正居中,而是偏左对齐。这是因为 verbatim 环境会忽略外部的 \centering 指令。
典型症状:
- 第 3 页的 ASCII 图表偏左
- 代码块没有居中显示
- 使用
center环境包裹verbatim无效
解决方案
核心方法
使用 varwidth 包包裹 verbatim 环境,再用 center 环境实现真正居中。
实现步骤
1. 添加宏包
在文档 preamble 中添加:
\usepackage{varwidth}
2. 重新定义 verbatim 环境
\let\originalverbatim\verbatim
\let\endoriginalverbatim\endverbatim
\renewenvironment{verbatim}{%
\par
\begin{center}
\begin{varwidth}{\linewidth}
\originalverbatim
\footnotesize
}{%
\endoriginalverbatim
\end{varwidth}
\end{center}
\par
}
3. 完整示例
\documentclass{article}
\usepackage{fontspec}
\usepackage{xeCJK}
\usepackage{varwidth} % 关键宏包
\setCJKmainfont{WenQuanYi Zen Hei}
% 重新定义 verbatim
\let\originalverbatim\verbatim
\let\endoriginalverbatim\endverbatim
\renewenvironment{verbatim}{%
\par
\begin{center}
\begin{varwidth}{\linewidth}
\originalverbatim
\footnotesize
}{%
\endoriginalverbatim
\end{varwidth}
\end{center}
\par
}
\begin{document}
这里是正常文本。
\begin{verbatim}
def main():
print("Hello World")
\end{verbatim}
这里也是正常文本。
\end{document}
技术原理
varwidth环境会根据内容自动调整宽度(最大不超过\linewidth)center环境会将varwidth盒子居中- 组合使用实现真正的居中效果
替代方案对比
| 方法 | 效果 | 推荐度 |
|---|---|---|
center + varwidth |
✅ 完美居中 | ⭐⭐⭐⭐⭐ |
center + minipage |
❌ 仍然偏左 | ⭐⭐ |
xleftmargin=0.5\linewidth |
❌ 会偏右 | ⭐ |
lrbox + center |
⚠️ 有时有效 | ⭐⭐⭐ |
注意事项
- 必须使用
varwidth包,标准minipage无效 - 需要编译两次才能看到最终效果
- 如果文档中有多个 verbatim,只需重定义一次
- 与
fancyvrb包可能冲突,建议二选一
文员专用说明
适用人员:文档排版文员、技术文档编辑
使用频率:当发现文档中代码块或 ASCII 图表未居中时使用
操作步骤:
- 检查文档是否已加载
varwidth包 - 检查是否已重定义
verbatim环境 - 如未配置,按上述步骤添加
- 编译两次 PDF 查看效果
相关文件
- 技能位置:
~/.openclaw/extensions/openclaw-latex-skills/skills/latex-verbatim-center/SKILL.md - 示例文档:
/home/raolin/.openclaw/workspace-editor/PIM-SSMoE_Research_Report.tex - 最终版本:
fixed42.pdf(使用此方案的第一个成功版本)
版本历史
- v1.0 (2026-04-03): 初始版本,基于 fixed42.pdf 成功经验
- 问题解决者:AI Assistant
- 问题报告者:用户
参考资料
- TeX Stack Exchange: "How to center verbatim" (2013)
- LaTeX.org Forum: "center a text in verbatim" (2009)
- CTAN: varwidth package documentation
- TeX Live 2024 验证通过
- Make sure OpenClaw is installed (local or Docker)
- Run the install command in chat:
/install latex-verbatim-center - After installation, invoke the skill by name or use
/latex-verbatim-center - Provide required inputs per the skill's parameter spec and get structured output
What is Latex Verbatim Center?
Redefines LaTeX verbatim to wrap with varwidth and center environments for proper horizontal centering of code blocks and ASCII art. It is an AI Agent Skill for Claude Code / OpenClaw, with 101 downloads so far.
How do I install Latex Verbatim Center?
Run "/install latex-verbatim-center" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.
Is Latex Verbatim Center free?
Yes, Latex Verbatim Center is completely free, licensed under MIT-0. You can download, install and use it at no cost.
Which platforms does Latex Verbatim Center support?
Latex Verbatim Center is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).
Who created Latex Verbatim Center?
It is built and maintained by Rao Lin (@bg1avd); the current version is v1.0.0.