如何在不干扰其他环境的情况下自定义现有LaTeX环境

时间:2009-03-03 15:10:02

标签: latex documentation-generation python-sphinx

我正在使用Sphinx来记录项目。它从restructured text生成LaTeX文件。

我想为提示备注设置灰色背景颜色,因此我在创建灰盒子环境后自定义了通知环境:

\definecolor{MyGray}{rgb}{0.80,0.80,0.80}

\makeatletter\newenvironment{graybox}{%
   \begin{lrbox}{\@tempboxa}\begin{minipage}{\columnwidth}}{\end{minipage}\end{lrbox}%
   \colorbox{MyGray}{\usebox{\@tempboxa}}
}\makeatother

\makeatletter
\renewenvironment{notice}[2]{
  \begin{graybox}
  \bf\it
  \def\py@noticetype{#1}
  \par\strong{#2}
  \csname py@noticestart@#1\endcsname
}
{
  \csname py@noticeend@\py@noticetype\endcsname
  \end{graybox}
}
\makeatother

除非我在 notice 环境中放置 figure 环境,否则一切正常。在这种情况下,我收到此错误:

  

LaTeX错误:不在外部标准模式

有没有办法为 notice 环境设置灰色背景?

3 个答案:

答案 0 :(得分:3)

这是FAQ。在灰色框内放置一个图形(或任何其他可以在输出中移动的“浮动”)是没有意义的;如果你想让你的图形包含一个灰色框,请将灰框环境置于图形环境中。

答案 1 :(得分:2)

感谢 godbyk Jouni 回答我的问题。

问题是我不直接在 LaTeX 中编码。我用重组文本编写文档,Sphinx输出 LaTeX 文件。

但是我找到了一个解决方案:我重新定义 figure 环境以使用 flowfram 包中的 staticfigure

\usepackage{flowfram}

\definecolor{MyGray}{rgb}{0.80,0.80,0.80}

\makeatletter\newenvironment{graybox}{%
   \begin{lrbox}{\@tempboxa}\begin{minipage}{\columnwidth}}{\end{minipage}\end{lrbox}%
   \colorbox{MyGray}{\usebox{\@tempboxa}}
}\makeatother

\makeatletter
\renewenvironment{notice}[2]{
  \begin{graybox}
  \bf\it
  \def\py@noticetype{#1}
  \par\strong{#2}
  \csname py@noticestart@#1\endcsname
}
{
  \csname py@noticeend@\py@noticetype\endcsname
  \end{graybox}
}
\makeatother

\renewenvironment{figure}[6]{
  \begin{staticfigure}
}{
  \end{staticfigure}
}

PS:重新定义'figure'时我不得不在参数数量上加6:如果我不这样做,它会在pdf文件中输出一些'htbp'(我不是 LaTeX expert。这只是我为这个问题找到的解决方案)

答案 2 :(得分:1)

正如Jouni正确指出的那样,数字和表格(即花车)可以四处移动,而你的灰色框子不能包含它们。要达到预期效果,您有两种选择:

  1. 将您的整个通知置于figure环境中(以便整个通知可以在页面上浮动,或者如果LaTeX选择的话,可以在新页面上浮动)。
  2. 不要使用浮动(figure环境) - 只需使用\includegraphics将图像直接弹出到notice环境中。但是,您将无法使用此非图形的标题,因为标题仅适用于图形或表格环境中。如果您想要与此图片相关联的标题,可以使用caption package

    \documentclass{article}
    \usepackage{caption}% let's us use captions outside of floats
    \usepackage{lipsum}% provides filler text
    \begin{document}
    \lipsum[1]
    \begin{center}
      \includegraphics{mypic}
      \captionof{figure}{This is my picture.}% makes a caption for non-floats
      \label{fig:mypic}
    \end{center}
    \lipsum[2]        
    \end{document}
    
  3. 我没有使用过Sphinx,所以我担心我无法帮助你将它集成到输出中。

相关问题