如何在不干扰其他环境的情况下自定义现有LaTeX环境
-
03-07-2019 - |
题
我正在使用 Sphinx 来记录项目。它从重组文本生成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
一切正常,除非我放置 数字通知环境中的环境。在这种情况下,我收到此错误:
LaTeX错误:不在外部标准模式
有没有办法为 notice 环境设置灰色背景?
解决方案 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。这只是我为这个问题找到的解决方案)
其他提示
这是常见问题解答。在灰色框内放置一个图形(或任何其他可以在输出中移动的“浮动”)是没有意义的;如果你想让你的图形包含一个灰色框,请将灰框环境置于图形环境中。
正如Jouni正确指出的那样,数字和表格(即花车)可以四处移动,而你的灰色框子不能包含它们。要达到预期效果,您有两种选择:
- 将您的整个通知放入
figure
环境中(以便整个通知可以在页面上浮动或者在LaTeX选择的情况下浮动到新页面。) -
不要使用float(
figure
环境) - 只需使用\ includegraphics
将图像直接弹出到notice
环境。但是,您将无法使用此非图形的标题,因为标题仅适用于图形或表格环境中。如果您需要与此图片相关联的标题,可以使用标题
包:\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}
醇>
我没有使用过Sphinx,所以我担心我无法帮助你将它集成到输出中。
不隶属于 StackOverflow