如何在整页中插入一条垂直线来分隔文本和边注?
答案1
以下代码仅在带有 marginpars 的页面上添加规则。如果它们出现在两侧(使用\reversemarginpar
和的混合\normalmarginpar
),则规则会在两侧显示。
定制的可能性包括规则本身(颜色、宽度等)通过以及通过\mparrule
给出的空间内的位置。\marginparsep
\mparrulefactor
\documentclass[twoside]{article}
\usepackage{etoolbox}
% patching the OR of LaTeX:
\makeatletter
\patchcmd{\@outputpage}%
{\box\@outputbox}%
{\hbox{%
\ifmpar@rule@lside
\hskip-\mparrulefactor\marginparsep\mparrule
\hskip\mparrulefactor\marginparsep
\fi
\box\@outputbox
\ifmpar@rule@rside
\hskip\mparrulefactor\marginparsep\mparrule
\fi}%
\global\mpar@rule@lsidefalse
\global\mpar@rule@rsidefalse
}%
{\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
\patchcmd{\@addmarginpar}%
{\global\setbox\@marbox\box\@currbox}%
{\global\setbox\@marbox\box\@currbox
\global\mpar@rule@lsidetrue
\else
\global\mpar@rule@rsidetrue
}%
{\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
\newif\ifmpar@rule@lside
\newif\ifmpar@rule@rside
\makeatother
\usepackage{color}
% \mparrule generates the \vrule but should use no space horizontally
% using color is just for fun ...
\newcommand\mparrule{\textcolor{blue}{\hskip-.2pt\vrule\hskip-.2pt}}
% placement factor: .5 places the rule midway in the space made available
% by \marginparsep
\newcommand\mparrulefactor{.5}
%\setlength\marginparsep{1cm}
\normalmarginpar
\usepackage{lipsum}
\begin{document}
\lipsum[1]\marginpar[where does this go?]{Test with some extra text to make more than one line}
\lipsum[2]
\newpage
\lipsum[1]\marginpar{Test with some extra text to make more than one line}
\newpage
\reversemarginpar % test odd placement .... not enough space in the default case
\lipsum[1]\marginpar[where does this go?]{Test with some extra text to make more than one line}
\normalmarginpar % and one on the other side as well
\lipsum[2]\marginpar[where does this go?]{Test with some extra text to make more than one line}
\newpage % no rule on the next page ...
\lipsum[1]
\lipsum[2]
\end{document}
解释:
我们\@addmarginpar
记录边距是添加到左侧还是右侧。一旦将其中一个开关设置为 true,它将一直保持这种状态,直到我们实际排版页面(这样,边距可以放在任一侧或两侧,我们就会得到正确的规则)。我们\@outputpage
只需使用开关在左侧和/或右侧添加规则即可\@outputbox
。通过将所有内容放入,\hbox
规则会自动扩展到框的整个高度。最后,我们将两个开关重置回 false,这样没有边距的页面就不会生成规则。
结果我们得到第一页:
第 3 页则显示了双方的规则:
变体
另外,也可以生成与边缘大小相匹配的线条。大多数以前的代码思路都可以重复使用,只是这次我们在 中完成所有操作\@addmarginpar
:
\usepackage{etoolbox}
% patching the OR of LaTeX:
\makeatletter
\patchcmd{\@addmarginpar}%
{\box \@marbox}%
{\hbox{%
\ifmpar@rule@rside
\hskip-\mparrulefactor\marginparsep\mparrule
\hskip\mparrulefactor\marginparsep
\fi
\box \@marbox
\ifmpar@rule@lside
\hskip\mparrulefactor\marginparsep\mparrule
\fi}%
\global\mpar@rule@lsidefalse
\global\mpar@rule@rsidefalse
}%
{\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
\patchcmd{\@addmarginpar}%
{\global\setbox\@marbox\box\@currbox}%
{\global\setbox\@marbox\box\@currbox
\global\mpar@rule@lsidetrue
\else
\global\mpar@rule@rsidetrue
}%
{\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
\newif\ifmpar@rule@lside
\newif\ifmpar@rule@rside
\makeatother
\usepackage{color}
% \marginparrule generates the \vrule but should use no space horizontally
% using color is just for fun ...
\newcommand\mparrule{\textcolor{blue}
{\hskip-2pt\vrule width 4pt\hskip-2pt}}
% placement factor: .5 places the rule midway in the space made available
% by \marginparsep
\newcommand\mparrulefactor{.4}
在这种情况下,我们会得到如下输出:
答案2
这是一个在所有页面中添加一个栏的解决方案(除非另有说明)。该栏使用 包排版为背景图片,eso-pic
并使用 绘制tikz
。可能有一种更简单的方法来实现它,但这种方法方便调整长度等。
关于长度,您将看到一个很好的意大利面条式代码示例,表明我不知道 latex 定义的一半长度。代码可以工作,但绝对不是最小的。
\documentclass[a4paper,twoside]{article}
\usepackage{lipsum}
\usepackage{eso-pic}
\usepackage{ifthen}
\usepackage{tikz}
\def\bottommargin{\paperheight - \topmargin - \textheight - \headheight - \headsep - 1in - \voffset}
\def\toptotalheight{\paperheight - \topmargin - \headheight - \headsep - 1in - \voffset}
\def\leftlength{
\evensidemargin - 0.5*\marginparsep
+ 1in
+ \hoffset
}
\def\rightlength{\paperwidth
- \evensidemargin + 0.5*\marginparsep
- 1in - \hoffset}
\makeatletter
\newcommand{\nomarginbar}{\let\ESO@HookIIBG\@empty}
\makeatother
\newcommand{\thisisfullsize}{\path (0,0) -- (\paperwidth,\paperheight);}
\newcommand\LeftBar{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
\vfill
\centering
\begin{tikzpicture}
\thisisfullsize
\draw[line width=1pt] (\leftlength,\bottommargin) -- (\leftlength,\toptotalheight);
\end{tikzpicture}
\vfill
}}}
\newcommand\RightBar{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
\vfill
\centering
\begin{tikzpicture}
\thisisfullsize
\draw[line width=1pt] (\rightlength,\bottommargin) -- (\rightlength,\toptotalheight);
\end{tikzpicture}
\vfill
}}}
%%% Use this in two-side documents
\AtBeginShipout{%
\ifthenelse{\isodd{\value{page}}}%
{\AddToShipoutPictureBG*{\LeftBar}%
}%
{\AddToShipoutPictureBG*{\RightBar}%
}%
}
% %%% Use this in one-side documents
% \AtBeginShipout{%
% \AddToShipoutPictureBG*{\RightBar}%
% }
%%% Use this anyway (to take care of the first page of the document)
\AtBeginDocument{%
\AddToShipoutPictureBG*{\RightBar}%
}
\begin{document}
\lipsum[1-6]
\marginpar{\lipsum[1]}
\lipsum[1-6]
\marginpar{\lipsum[1]}
\lipsum[1-6]
\end{document}
如果您希望某些页面没有栏,只需\nomarginbar
在该页面的某处插入即可。
结果是twoside
:
结果是oneside
:
(抱歉截图质量较差,以后我会努力做出更好的)