为每个页面自动执行一个命令

为每个页面自动执行一个命令

我的代码将环境内的组保存toprtopl两个sbox,命令\border绘制一个框架,分成两部分,并将两个部分放入其中sbox,这是我的 MWE

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\newsavebox{\textone}
\newsavebox{\texttwo}

\newenvironment{topr}{%
  \begin{lrbox}{\textone}%
  \begin{minipage}{11cm}%
  \raggedleft 
}{%
  \end{minipage}%
  \end{lrbox}%
  % export box register setting outside the scope of the environment
  \global\setbox\textone=\copy\textone
}

\newenvironment{topl}{%
  \begin{lrbox}{\texttwo}%
  \begin{minipage}{5cm}%
  \raggedleft 
}{%
  \end{minipage}%
  \end{lrbox}%
  % export box register setting outside the scope of the environment
  \global\setbox\texttwo=\copy\texttwo
}

\newcommand{\border}{%

  \begin{tikzpicture}[remember picture, overlay]

    % Coordinates of the external frame
    \coordinate (top) at ($(current page.north)+(0,-1.5)$);
    \coordinate (bottom) at ($(current page.south)+(0,1.5)$);
    \coordinate (left) at ($(current page.west)+(1.5,0)$);
    \coordinate (right) at ($(current page.east)+(-1.5,0)$);

    % Coordinates of the horizontal parts
    \coordinate (left part) at ($(left)!.33!(right)$);

    % Frame around
    \draw[red] (bottom-|left) rectangle (top-|right);

    % Vertical lines
    \draw[red] (top-|left part) -- (bottom-|left part);

    \node at ($(top-|right)+(-0.3,-0.3)$)[anchor=north east]{\usebox{\textone}};
    \node at ($(top-|left part)+(-0.3,-0.3)$)[anchor=north east]{\usebox{\texttwo}};      

 \end{tikzpicture}}

\begin{document}

\begin{topr}
 \lipsum[1]
\end{topr}

\begin{topl}
 \lipsum[2]
\end{topl}

\border  

\newpage

\border 

\end{document} 

我们得到

在此处输入图片描述

我想改进这个宏,以便可以实现到我的文档的每一页,并且不用对每一页都说,在从一页到另一页的每次转换\border中擦除文本。sbox

答案1

我建议使用eso-pic它允许您使用多种不同的宏在页面发货时添加内容:

在此处输入图片描述

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz,eso-pic}
\usetikzlibrary{calc}
\pagestyle{empty}
\newsavebox{\textone}
\newsavebox{\texttwo}

\newenvironment{topr}{%
  \begin{lrbox}{\textone}%
  \begin{minipage}{11cm}%
  \raggedleft 
}{%
  \end{minipage}%
  \end{lrbox}%
  % export box register setting outside the scope of the environment
  \global\setbox\textone=\copy\textone
}

\newenvironment{topl}{%
  \begin{lrbox}{\texttwo}%
  \begin{minipage}{5cm}%
  \raggedleft 
}{%
  \end{minipage}%
  \end{lrbox}%
  % export box register setting outside the scope of the environment
  \global\setbox\texttwo=\copy\texttwo%
  \mbox{}% Just set something on the page
}

\newcommand{\border}{%
  \begin{tikzpicture}[remember picture, overlay]
    % Coordinates of the external frame
    \coordinate (top) at ($(current page.north)+(0,-1.5)$);
    \coordinate (bottom) at ($(current page.south)+(0,1.5)$);
    \coordinate (left) at ($(current page.west)+(1.5,0)$);
    \coordinate (right) at ($(current page.east)+(-1.5,0)$);

    % Coordinates of the horizontal parts
    \coordinate (left part) at ($(left)!.33!(right)$);

    % Frame around
    \draw[red] (bottom-|left) rectangle (top-|right);

    % Vertical lines
    \draw[red] (top-|left part) -- (bottom-|left part);

    \node at ($(top-|right)+(-0.3,-0.3)$)[anchor=north east]{\usebox{\textone}};
    \node at ($(top-|left part)+(-0.3,-0.3)$)[anchor=north east]{\usebox{\texttwo}};      
  \end{tikzpicture}}

\begin{document}

{\Huge This is a different first page.}

\newpage

\makeatletter
\AddToShipoutPictureFG{%
  \ifdim\dimexpr\ht\textone+\ht\texttwo=0pt\else
    \border
    \global\setbox\textone=\copy\voidb@x
    \global\setbox\texttwo=\copy\voidb@x
  \fi}
\makeatother

\begin{topr}
 \lipsum[1]
\end{topr}

\begin{topl}
 \lipsum[2]
\end{topl}

\newpage

\begin{topr}
 \lipsum[2]
\end{topr}

\begin{topl}
 \lipsum[1]
\end{topl}

\end{document} 

相关内容