如何自定义现有的LaTeX环境而不干扰其他环境?

发布于 2024-07-14 14:45:44 字数 1091 浏览 7 评论 0原文

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

我想为 tipsnotes 设置灰色背景颜色,因此我在创建灰盒环境后自定义了 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 技术交流群。

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

发布评论

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

评论(3

神也荒唐 2024-07-21 14:45:44

这是常见问题解答。 将图形(或任何其他可以在输出中移动到其他位置的“浮动”)放入灰框中是没有意义的; 如果您希望图形包含灰盒,请将灰盒环境放入图形环境中。

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.

像极了他 2024-07-21 14:45:44

感谢godbykJouni回答我的问题。

问题是我不直接用 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:我必须将 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:

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

水中月 2024-07-21 14:45:44

正如 Jouni 正确指出的那样,数字和表格(即浮动)可以移动,而你的灰盒子不能包含它们。 要达到所需的效果,您有两个选择:

  1. 将整个通知放入 figure 环境中(以便整个通知可以在页面上浮动,或者如果 LaTeX 选择的话,可以浮动到新页面)。
  2. 不要使用浮动(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:

  1. Put your entire notice into a figure environment (so that the entire notice can be floated around on the page or to a new page if LaTeX so chooses).
  2. Don't use a float (figure environment) -- just use \includegraphics to pop your image directly into the notice 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 the 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}
    

I haven't used Sphinx, so I'm afraid I can't help you too much with integrating this into their output.

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