如何自定义现有的LaTeX环境而不干扰其他环境?
我正在使用 Sphinx 来记录项目。 它从 reStructuredText 生成 LaTeX 文件。
我想为 tips 和 notes 设置灰色背景颜色,因此我在创建灰盒环境后自定义了 notice 环境:
\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 错误:不在外部模式
有没有办法为该通知环境设置灰色背景?
I'm using Sphinx for documenting a project. It produces LaTeX files from reStructuredText.
I would like to set a gray background color to the tips and notes, so I customized the notice environment after creating a graybox environment:
\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
Everything works fine except if I place a figure environment inside the notice environment. In that case, I get this error:
LaTeX Error: Not in outer par mode
Is there a way to set a gray background to that notice environment ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是常见问题解答。 将图形(或任何其他可以在输出中移动到其他位置的“浮动”)放入灰框中是没有意义的; 如果您希望图形包含灰盒,请将灰盒环境放入图形环境中。
This is a FAQ. It does not make sense to put a figure (or any other "float" which can move elsewhere in the output) inside a gray box; if you want your figure to contain a gray box, put the gray box environment inside the figure environment.
感谢godbyk和Jouni回答我的问题。
问题是我不直接用 LaTeX 编码。 我用重组文本编写文档,Sphinx 输出 LaTeX 文件。
但我找到了一个解决方案:我重新定义了 figure 环境以使用 flowfram 包中的 staticfigure:
PS:我必须将 6 放入重新定义“figure”时的参数数量:如果我不这样做,它会在 pdf 文件中输出一些“htbp”(我不是 LaTeX 专家。这只是我为此找到的解决方案问题)
Thank you godbyk and Jouni for answering my question.
The problem is that I don't code directly in LaTeX. I write the documentation in restructured text and Sphinx output the LaTeX files.
But I found a solution: I redefine the figure environment to use the staticfigure from the flowfram package:
PS: I had to put 6 to the number of arguments when redefining 'figure': if I don't do that it outputs some 'htbp' in the pdf files (I'm not a LaTeX expert. it's just the solution I found for this problem)
正如 Jouni 正确指出的那样,数字和表格(即浮动)可以移动,而你的灰盒子不能包含它们。 要达到所需的效果,您有两个选择:
figure
环境中(以便整个通知可以在页面上浮动,或者如果 LaTeX 选择的话,可以浮动到新页面)。不要使用浮动(
figure
环境)——只需使用\includegraphics
将图像直接弹出到notice
环境中。 但是,您将无法在该非图形中使用标题,因为标题只能在图形或表格环境中使用。 如果您想要与此图像关联的标题,可以使用标题
包:<前><代码>\documentclass{文章}
\usepackage{caption}% 让我们在浮动之外使用标题
\usepackage{lipsum}% 提供填充文本
\开始{文档}
\lipsum[1]
\开始{中心}
\includegraphics{mypic}
\captionof{figure}{这是我的图片。}% 为非浮动元素添加标题
\标签{图:mypic}
\结束{中心}
\lipsum[2]
\结束{文档}
我没有使用过 Sphinx,所以恐怕我无法帮助您将其集成到他们的输出中。
As Jouni correctly pointed out, figures and tables (i.e., floats) can be moved around, and your gray box can't contain them. To achieve the desired effect, you have two options:
figure
environment (so that the entire notice can be floated around on the page or to a new page if LaTeX so chooses).Don't use a float (
figure
environment) -- just use\includegraphics
to pop your image directly into thenotice
environment. You won't be able to use a caption with this non-figure, however, as captions only work inside a figure or table environment. If you want a caption associated with this image, you can use thecaption
package:I haven't used Sphinx, so I'm afraid I can't help you too much with integrating this into their output.