首页 > 解决方案 > 是否可以使用 LaTeX 包“子文件”和“背景”?

问题描述

我在我的 LaTeX 项目中使用包“子文件”和“背景”时遇到了问题。

我有一个如下所示的主文件:

\documentclass[10pt, a4paper, parskip]{scrartcl}
\usepackage[pages=some, angle=0, opacity=1, scale=1]{background}
\usepackage{subfiles}
\definecolor{background}{RGB}{204, 68, 68}

\begin{document}
    \subfile{subfile1}
    \subfile{subfile2}
\end{document}

我想包含两个子文件。第一个子文件如下所示:

\documentclass[main.tex]{subfiles}

\begin{document}
    \backgroundsetup{
        contents={
            \begin{tikzpicture}[overlay]
                \path [fill=background] (-.5\paperwidth, 6) rectangle (.5\paperwidth, 9.25);
            \end{tikzpicture}
        }
    }
    \BgThispage
    Text...
\end{document}

第二个子文件看起来相似,但具有不同的背景设置:

\documentclass[main.tex]{subfiles}

\begin{document}
    \backgroundsetup{
        contents={
            \begin{tikzpicture}[overlay]
                \path [fill=background] (-.5\paperwidth, -12.5) rectangle (.5\paperwidth, 12.5);
            \end{tikzpicture}
        }
    }
    \BgThispage
    Text...
\end{document}

将这两个子文件编译为独立文件会给我预期的结果。但是编译主文件并没有设置正确的背景,即使文件编译没有错误。

是否可以修复此错误,或者是否有可以协同工作的包“子文件”或“背景”的替代方案?

编辑:我将主文件更改为:

...
\begin{document}
    \subfile{subfile1}
    \newpage
    \subfile{subfile2}
\end{document}
...

现在我在编译主文件时只看到水印说“草稿”,但它被设置为正确的页面。

编辑:我发现如果包含的子文件只有一页长,就会出现水印。如果内容至少有两页长,则背景会正确显示。有谁知道为什么?

标签: backgroundpackageincludelatex

解决方案


我找到了解决问题的方法。在主文件中删除 \newpage 命令,并在每个子文件中以 \newpage 命令结束代码。这样,主文件和所有子文件作为独立文件的编译工作正常。

是告诉我的评论。


推荐阅读