我有一份如下所示的一页文档。我希望背景颜色为绿色,然后我想将其更改为黄色。可以吗?
\documentstyle{article}
\begin{document}
\pagecolor{green}
I would like the background color of the page until here would be green.\\
\pagecolor{yellow}
Then from now on, I would like the background would be yellow.
\end{document}
答案1
从当前位置更改页面颜色的命令不需要知道确切位置。相反,可以绘制一个大的彩色矩形。以下示例\paperwidth
向左移动并绘制一个宽度2\paperwidth
和高度为的矩形\paperheight
。无论页面上的当前位置如何,这都会覆盖剩余页面的整个部分。不需要额外运行 LaTeX。
绘制矩形后,\pagecolor
里面使用\afterpage
也可以获取实际页面颜色的改变。
该命令支持或包括可选参数\changepagecolor
的完整语法。\color
\pagecolor
\documentclass{article}
\usepackage[a6paper, hmargin=12mm, vmargin=12mm, includefoot]{geometry}
\usepackage{lipsum}
\usepackage{color}
\usepackage{afterpage}
\makeatletter
% Macro \changepagecolor has the same syntax as \pagecolor or \color
% with an optional argument and a mandatory argument.
\newcommand*{\changepagecolor}{%
\@ifnextchar[\@changepagecolor@i\@changepagecolor@ii
}
% Case: \changepagecolor[...]{...}
\def\@changepagecolor@i[#1]#2{%
\@changepagecolor@do{[{#1}]{#2}}%
}
% Case: \changepagecolor{...}
\newcommand*{\@changepagecolor@ii}[1]{%
\@changepagecolor@do{{#1}}%
}
\newcommand*{\@changepagecolor@do}[1]{%
% Fill the remaining space with a colored rule
\begingroup
\offinterlineskip
\hbox to 0pt{%
\kern-\paperwidth
\vtop to 0pt{%
\color#1%
\hrule width 2\paperwidth height \paperheight
\vss
}%
\hss
}%
\endgroup
% Set page color for the next page
\afterpage{\pagecolor#1}%
}
\makeatother
\begin{document}
\pagecolor{green}
\lipsum[2]
\changepagecolor{yellow}
\lipsum[3]
\lipsum[4]
\end{document}
答案2
这是一种使用tikzmark
并避免需要手动指定每个矩形高度的方法。
\documentclass{article}
\usepackage{tikz,kantlipsum}
\usetikzlibrary{tikzmark}
\begin{document}
\pagecolor{green}
\kant[1]
\tikzmark{here}
\begin{tikzpicture}[overlay, remember picture, inner sep=0pt, outer sep=0pt]
\fill [yellow] (current page.west |- {pic cs:here}) rectangle (current page.south east);
\end{tikzpicture}
\kant[2]
\end{document}
答案3
\documentclass{article}
\usepackage{tikz}
\newcommand{\amount}{6in} %%<---- adjust
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\fill[green] ([yshift=-\amount]current page.north west) rectangle (current page.north east);
\fill[yellow] (current page.south west) rectangle ([yshift=-\amount]current page.north east);
\end{tikzpicture}
I would like the background color of the page until here would be green.\\
\vspace{4in}
Then from now on, I would like the background would be yellow.
\end{document}
答案4
通常情况下,必须在定义所有 \tikzmarks 之后绘制 tikz 图片,在这种情况下,它们将绘制在文本之上,因此对于此问题,必须小心地从上到下进行操作。但是,使用页面钩子,可以在页面布局之后但在页面输出之前绘制 tikz 图片,这会将文本置于顶部。
\documentclass{article}
\usepackage{everypage}
\usepackage{tikzpagenodes}
\begin{document}
\tikz[remember picture,overlay]{\path coordinate(A);}%
I would like the background color of the page until here would be green.\\
Then from now on, I would like the background would be yellow.
\AddThispageHook{\begin{tikzpicture}[remember picture,overlay]
\fill[color=green] (A -| current page.west) rectangle (current page.north east);
\fill[color=yellow] (A -| current page.west) rectangle (current page.south east);
\end{tikzpicture}}
\end{document}