我使用一个模板来创建包含两页的文档。第一页有垂直条,第二页有水平条。我想扩展此模板,以便在第一页后添加任意数量的带有垂直条的页面。
我想出了使用 atbegshi 的以下方法:
\documentclass[a4paper]{article}
\usepackage[margin=1.0cm,noheadfoot]{geometry}
\usepackage{atbegshi,tikz,lipsum}
\pagestyle{empty}
\AtBeginShipout{\ifnum\value{page}>0%
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\fi}
\begin{document}
% Dummy code for first template page with vertical bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template page with vertical bar}
\end{center}
\vspace*{\fill}
%\clearpage
% Additional pages with sidebar and dummy text
\newgeometry{left=6.0cm,right=1.0cm,top=1.0cm,bottom=1.0cm,noheadfoot}
\begin{center}
\LARGE{Additional pages with vertical bar}
\end{center}
\lipsum[1-20]
\restoregeometry
%\clearpage
% Dummy code for last template page with horizontal bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(\paperwidth,-6.0cm);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template pages with horizontal bar}
\end{center}
\vspace*{\fill}
\end{document}
原始模板也使用 TikZ 来创建条形图。我的方法有两个问题。由于条件简单\ifnum\value{page}>0
,垂直条也应用于水平条的页面。此外,在文档末尾添加了一个空白页。
\newgeometry{...}
我首选的解决方案是使用类似于和 的开关来打开和关闭垂直条\restoregeometry
。这可用于定义新环境。简单的布尔值似乎不起作用。
有没有办法实现这样的转换或者更好的解决这个问题?
答案1
与我原来的说法相反,可以使用布尔值来解决这个问题。这是我现在使用的代码。
\documentclass[a4paper]{article}
\usepackage[margin=1.0cm,noheadfoot]{geometry}
\usepackage{atbegshi,tikz,lipsum,ifthen}
\pagestyle{empty}
\newboolean{drawSidebar}
\setboolean{drawSidebar}{false}
\AtBeginShipout{\ifthenelse{\boolean{drawSidebar}}{
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
}{}}
\begin{document}
% Dummy code for template page with vertical bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template page with vertical bar}
\end{center}
\vspace*{\fill}
%\clearpage
% Additional pages with sidebar and dummy text
\setboolean{drawSidebar}{true}
\newgeometry{left=6.0cm,right=1.0cm,top=1.0cm,bottom=1.0cm,noheadfoot}
\begin{center}
\LARGE{Additional pages with vertical bar}
\end{center}
\lipsum[1-20]
\setboolean{drawSidebar}{false}
\restoregeometry
%\clearpage
% Dummy code for template page with horizontal bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(\paperwidth,-6.0cm);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template pages with horizontal bar}
\end{center}
\vspace*{\fill}
\end{document}