对 LaTeX 环境进行小修改
我在整个 LaTeX 文档中一直使用 \begin{figure} ... \end{figure}
,但默认样式很丑陋;也就是说,数字都是左对齐的。有没有办法重新定义“figure”环境,以便它自动插入一些居中命令,例如这样?:
\begin{figure} \begin{center}
\end{center} \end{figure}
当然,我可以使用 \newenvironment
来定义“cfigure”环境,但这是不可取的。我不想遍历并将所有“数字”更改为“cfigures”(后来意识到我希望所有数字都右对齐,并且必须将它们全部重命名为“rfigures”)。
我可以使用 \renewenvironment
,但随后我必须挖掘 LaTeX 源代码来查找“图形”环境最初定义为复制/粘贴的内容。
我几乎 在 这篇博文 中找到了我想要的内容,但是那里的示例是命令,而不是环境。
I've been using \begin{figure} ... \end{figure}
throughout my LaTeX document, but the default styling is ugly; namely, the figures are all left-aligned. Is there a way to redefine the "figure" environment so it automatically inserts some centering commands such as like this?:
\begin{figure} \begin{center}
\end{center} \end{figure}
Sure, I could use \newenvironment
to define a "cfigure" environment, but that's undesirable. I don't want to go through and change all my "figures" to "cfigures" (and then later realise I wanted all the figures to be right-aligned and have to rename them all to "rfigures").
I could use \renewenvironment
, but then I'd have to dig through the LaTeX source to find what the "figure" environment was originally defined as copy/paste it in.
I almost found what I wanted at this blog post, but the example there was for a command, not an environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个使用可选参数的解决方案。
已修复。
已修复 2. 它确实可以与任何选项、任何规则以及内部的
\par
配合良好。Another solution which works with the optional arguments.
Fixed.
Fixed 2. It does work well with any options and any rules and
\par
inside.正如另一个答案中所述,您不能使用将命令添加到
\figure
宏末尾的老技巧,因为这会扰乱可选参数处理。如果环境没有参数,那么它会正常工作,但否则没有直接的方法可以做到这一点。
对于您的图形问题,请尝试加载 floatrow 包:
如果会自动将图形内容居中。
更新:如果您不想加载包,这里有一些代码也可以做到这一点。请注意,它特定于
figure
环境,但基本主题是:复制原始定义,以相同的方式解析参数,然后在末尾添加所需的任何代码。\edef
需要在传递给\@float
宏之前完全展开\fps@figure
。As noted in another answer, you can't do the old trick of prepending commands to the end of the
\figure
macro because that will mess up the optional argument processing.If an environment doesn't have arguments then it will work fine, but otherwise there's no straightforward way of doing this.
For your problem with the figures, try loading the floatrow package:
If will centre the content of your figures automatically.
Update: If you don't want to load a package, here's some code that will also do it. Note that it's specific to the
figure
environment, but the basic theme is: copy the original definition, parsing arguments the same way, then add whatever code you need at the end.The
\edef
is required to fully expand\fps@figure
before it's passed to the\@float
macro.怎么样:
注意:未经测试。
How about:
Note: untested.