在页边空白处用线标记条件版本

在页边空白处用线标记条件版本

我有两个版本的文档,我使用versions包。所以我可以使用\includeversion/\excludeversion从同一源生成不同的版本。

我想生成一个单一的文档,其中既不包含也不排除条件材料,但是标记。版本包提供了\markversion命令和钩子\beginmarkversion\endmarkversion用于此目的。我希望我的标记是一条垂直线,沿着整个条件材料的边缘运行。

这是我目前的实现。我知道这可能是对 TikZ 的滥用,但这是我所知道的唯一开始方式:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{versions}
% customize the marks of beginning and ending version
\makeatletter
%% TikZ style
  \newcounter{versionmark}
  \renewcommand{\beginmarkversion}{%
    \stepcounter{versionmark}%
    \begin{tikzpicture}[overlay, remember picture]
      \node[coordinate] (bv\theversionmark) at ([xshift=2cm]0,0 -| current page.south west) {};
    \end{tikzpicture}%
  }
  \renewcommand{\endmarkversion}{%
    \edef\theversion{\@currenvir}
    \begin{tikzpicture}[overlay, remember picture]
      \node[coordinate] (ev\theversionmark) at ([xshift=2cm]0,0 -| current page.south west) {};
      \draw[red!80!black] 
        (ev\theversionmark) 
        -- (bv\theversionmark) node[midway,sloped,anchor=south] {\theversion};
      \draw[red!80!black]
        (ev\theversionmark) --++ (1ex,0cm)
        (bv\theversionmark) --++ (1ex,0cm);
    \end{tikzpicture}%  
  }
\makeatother
\markversion{A}
\markversion{B}
%\includeversion{A}
%\excludeversion{B}

\begin{document}


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

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

\begin{A}
\lipsum
\end{A}


\end{document}

这在前两种情况下有效,但在第三种情况下无效,因为条件材料的长度超过一页。当\end{A}找到第二个时,行不是从第二个 的位置开始\begin{A},而是从 页面上\end{A}与 位置相同的位置开始\begin{A}

示例页 1 示例页 2

我该如何修复这个问题,以便该行从条件材料的开头开始,到条件材料的结尾结束?

答案1

我按照 Seamus 的指示,找到了 Martin 的回答测试某个段落是否有分页符?并得到了一些效果很好的结果:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{versions}
\usepackage{zref-abspage}
\usepackage{zref-user}
\usepackage{tikz}
\usepackage{atbegshi}
\usetikzlibrary{calc}

\makeatletter
\newcommand{\currentsidemargin}{%
  \ifodd\zref@extract{textarea-\thetextarea}{abspage}%
    \oddsidemargin%
  \else%
    \evensidemargin%
  \fi%
}

\newcounter{textarea}
\newcommand{\settextarea}{%
   \stepcounter{textarea}%
   \zlabel{textarea-\thetextarea}%
   \begin{tikzpicture}[overlay,remember picture]
    % Helper nodes
    \path (current page.north west) ++(\hoffset, -\voffset)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, minimum height=\paperheight]
        (pagearea) {};
    \path (pagearea.north west) ++(1in+\currentsidemargin,-1in-\topmargin-\headheight-\headsep)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\textwidth, minimum height=\textheight]
        (textarea) {};
  \end{tikzpicture}%
}

\tikzset{tikzborder/.style={line width=1mm,red,double=blue}}

\newcounter{tikzborder}
\newcounter{tikzborderpages}
\newenvironment{tikzborder}[1][]{%
    \medskip\par
    % Allow user to overwrite the used style locally
    \ifx&#1&\else
        \tikzset{tikzborder/.style={#1}}%
    \fi
    \settextarea
    \stepcounter{tikzborder}%
    \tikz[overlay,remember picture] \coordinate (tikzborder-\thetikzborder);% Modified \tikzmark macro
    \zlabel{tikzborder-begin-\thetikzborder}%
    % Test if end-label is at the same page and draw first half of border if not
    \ifnum\zref@extract{tikzborder-begin-\thetikzborder}{abspage}=\zref@extract{tikzborder-end-\thetikzborder}{abspage} \else
        \begin{tikzpicture}[overlay,remember picture]
            \draw [tikzborder]
                let \p0 = (textarea.north west), \p1 = (tikzborder-\thetikzborder), \p2 = (textarea.south east) in
                (\x0-\fboxsep-.5\pgflinewidth,\y2-\fboxsep-.5\pgflinewidth) 
                 --
                (\x0-\fboxsep-.5\pgflinewidth,\ht\strutbox+\fboxsep+.5\pgflinewidth) 
                node[midway,anchor=east] {\currver}
                ;
        \end{tikzpicture}%
    % If it spreads over more than two pages:
    \setcounter{tikzborderpages}{\numexpr-\zref@extract{tikzborder-begin-\thetikzborder}{abspage}+\zref@extract{tikzborder-end-\thetikzborder}{abspage}}
    \ifnum\value{tikzborderpages}>1
        \AtBeginShipoutNext{\tikzborderpage}%
    \fi
    \fi
}{%
    \zlabel{tikzborder-end-\thetikzborder}%
    % Test if begin-label is at the same page and draw whole border if so
    \ifnum\zref@extract{tikzborder-begin-\thetikzborder}{abspage}=\zref@extract{tikzborder-end-\thetikzborder}{abspage}
        \begin{tikzpicture}[overlay,remember picture]
            \draw [tikzborder]
                let \p0 = (textarea.north west), \p1 = (tikzborder-\thetikzborder), \p2 = (textarea.south east) in
                (\x0-\fboxsep-.5\pgflinewidth,\y1+\ht\strutbox+\fboxsep+.5\pgflinewidth)
                    --
                (\x0-\fboxsep-.5\pgflinewidth,-\dp\strutbox-\fboxsep-.5\pgflinewidth)
                node [midway,anchor=east] {\currver};
                ;
        \end{tikzpicture}%
    % Otherwise draw second half of border
    \else
        \settextarea
        \begin{tikzpicture}[overlay,remember picture]
            \draw [tikzborder]
                let \p0 = (textarea.north west), \p1 = (tikzborder-\thetikzborder), \p2 = (textarea.south east) in
                (\x0-\fboxsep-.5\pgflinewidth,\y0+\fboxsep+.5\pgflinewidth)
                 --
                (\x0-\fboxsep-.5\pgflinewidth,-\dp\strutbox-\fboxsep-.5\pgflinewidth)
                node[midway,anchor=east] {\currver}
                ;
        \end{tikzpicture}%
    \fi
    \par\medskip
}

\newcommand{\tikzborderpage}{%
  \settextarea
  \begin{tikzpicture}[overlay,remember picture]
      \draw [tikzborder]
          ([shift={(-\fboxsep-.5\pgflinewidth, \fboxsep+.5\pgflinewidth)}]textarea.north west)
           --
          ([shift={(-\fboxsep-.5\pgflinewidth,-\fboxsep-.5\pgflinewidth)}]textarea.south west)
          node[midway,anchor=east] {\currver}
          ;
  \end{tikzpicture}%
  \addtocounter{tikzborderpages}{-1}%
  \ifnum\value{tikzborderpages}>1
    \AtBeginShipoutNext{\tikzborderpage}%
  \fi
  \vspace{-\baselineskip}% Compensate for the generated extra line at begin of the page. No idea why exactly this happens. Also \baselineskip seems not to be 100% right.
}
\makeatother

\usepackage{lipsum}
\newcommand\xlipsum[1][]{{\let\par\relax\lipsum*[#1]}}

% customize the marks of beginning and ending version
\makeatletter
  \newcounter{versionmark}
  \renewcommand{\beginmarkversion}{%
    \makeatletter
        \edef\currver{\@currenvir}
    \makeatother
    \begin{tikzborder}
  }
  \renewcommand{\endmarkversion}{%
    \end{tikzborder}
  }
\makeatother
\markversion{A}
\markversion{B}

\begin{document}


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

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

\begin{A}
\lipsum
\end{A}

\begin{B}
\lipsum
\lipsum
\end{B}

\end{document}

我确信某些pgfkeys魔法会让它变得更加灵活。

答案2

我不是 TeX 专家,但是你为什么不使用这个changes包呢,看这里:

http://changes.sourceforge.net/

相关内容