答案1
这使用了\AddToHook{shipout/background}
,它相对较新并且内置于 LaTeX 中。
请注意,shipout\value{page}
是可靠的。只有在使用浮动或页面最后一段(好吧,任何可能排在最后的段落)时,才需要 changepage 或 ifoddpage 之类的东西。
\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{xcolor}
\definecolor{MyBlue}{HTML}{0053A6}
\newcommand\ColorMargin{%
\ifodd\value{page}%
\put (1 cm,-1cm){% relative to upper left corner
\begin{tikzpicture}[baseline=(current bounding box.north)]
\node[rotate=90, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}%
\else
\put (\paperwidth - 2 cm,-1cm){%
\begin{tikzpicture}[baseline=(current bounding box.north)]
\node[rotate=270, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}%
\fi
}
\AddToHook{shipout/background}[myhook]{\ColorMargin}
\begin{document}
\sloppy\lipsum[1-50]\lipsum[1-50]
\RemoveFromHook{shipout/background}[myhook]
\lipsum[1-50]
\end{document}
答案2
对我的问题的评论让我得到了很多不同的答案。我使用以下方法从中编译了一个 MWE eso-picture
:
\documentclass{article}[a4paper]
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{changepage}
\strictpagecheck
\usepackage{xcolor}
\usepackage{eso-pic}
\definecolor{MyBlue}{HTML}{0053A6}
\newcommand\ColorMargin{%
\checkoddpage
\ifoddpage
\put (1 cm,480){
\begin{tikzpicture}
\node[rotate=90, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}
\else
\put (\paperwidth - 2 cm,480){
\begin{tikzpicture}
\node[rotate=270, minimum height=1 cm, minimum width=10 cm, fill=MyBlue, text=white] at (0, 0) {\bfseries\large MY TEXT};
\end{tikzpicture}}
\fi
}
\AddToShipoutPicture{\ColorMargin}
\begin{document}
\sloppy\lipsum[1-50]\lipsum[1-50]
\ClearShipoutPicture
\lipsum[1-50]
\end{document}