对 LaTeX 环境进行小修改

发布于 2024-08-07 12:55:03 字数 596 浏览 2 评论 0原文

我在整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落成白 2024-08-14 12:55:03
\let\oldfigure\figure
\def\figure{\oldfigure\centering}

另一个使用可选参数的解决方案。

已修复。

\let\oldfigure\figure
\let\oldendfigure\endfigure
\def\figure{\begingroup \oldfigure}
\def\endfigure{\centering \oldendfigure \endgroup}

已修复 2. 它确实可以与任何选项、任何规则以及内部的 \par 配合良好。

\makeatletter
\let\oldfigure\figure
\def\figure{\@ifnextchar[\figure@i \figure@ii}
\def\figure@i[#1]{\oldfigure[#1]\centering}
\def\figure@ii{\oldfigure\centering}
\makeatother
\let\oldfigure\figure
\def\figure{\oldfigure\centering}

Another solution which works with the optional arguments.

Fixed.

\let\oldfigure\figure
\let\oldendfigure\endfigure
\def\figure{\begingroup \oldfigure}
\def\endfigure{\centering \oldendfigure \endgroup}

Fixed 2. It does work well with any options and any rules and \par inside.

\makeatletter
\let\oldfigure\figure
\def\figure{\@ifnextchar[\figure@i \figure@ii}
\def\figure@i[#1]{\oldfigure[#1]\centering}
\def\figure@ii{\oldfigure\centering}
\makeatother
酒废 2024-08-14 12:55:03

正如另一个答案中所述,您不能使用将命令添加到 \figure 宏末尾的老技巧,因为这会扰乱可选参数处理。

如果环境没有参数,那么它会正常工作,但否则没有直接的方法可以做到这一点。

对于您的图形问题,请尝试加载 floatrow 包:

\usepackage{floatrow}

如果会自动将图形内容居中。

更新:如果您不想加载包,这里有一些代码也可以做到这一点。请注意,它特定于 figure 环境,但基本主题是:复制原始定义,以相同的方式解析参数,然后在末尾添加所需的任何代码。

\makeatletter
\renewenvironment{figure}[1][\fps@figure]{
  \edef\@tempa{\noexpand\@float{figure}[#1]} 
  \@tempa\centering
}{
  \end@float
}
\makeatother

\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:

\usepackage{floatrow}

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.

\makeatletter
\renewenvironment{figure}[1][\fps@figure]{
  \edef\@tempa{\noexpand\@float{figure}[#1]} 
  \@tempa\centering
}{
  \end@float
}
\makeatother

The \edef is required to fully expand \fps@figure before it's passed to the \@float macro.

你如我软肋 2024-08-14 12:55:03

怎么样:

\newenvironment{centeredfigure}{\begin{figure}\begin{center}}{\end{center}\end{figure}}

注意:未经测试。

How about:

\newenvironment{centeredfigure}{\begin{figure}\begin{center}}{\end{center}\end{figure}}

Note: untested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文