开发者

Making a small modification to a LaTeX environment

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 开发者_JAVA百科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.


\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


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.


How about:

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

Note: untested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜