![在文档中定义变量,以便宏定义进入序言](https://linux22.com/image/394294/%E5%9C%A8%E6%96%87%E6%A1%A3%E4%B8%AD%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F%EF%BC%8C%E4%BB%A5%E4%BE%BF%E5%AE%8F%E5%AE%9A%E4%B9%89%E8%BF%9B%E5%85%A5%E5%BA%8F%E8%A8%80.png)
我编写了一些宏来将 OMR 字符添加到生成的文档中。信封插入机需要 OMR 字符。
当我想为双面文档打印 OMR 字符时,我必须输入:
\DoubleSidedPrinting
\AddOMR
我想将我的宏放在一个将被预编译并可重复用于多个文档的序言中。在这种情况下,我只需在每个文档中设置 DoubleSidedPrinting 和 AddOMR。但这行不通。您知道解决方法吗?
% The goal is to put this code in a reusable preamble
\documentclass[12pt,twoside,a4paper]{article}
\usepackage{eso-pic}
\usepackage{rotating}
\usepackage{graphicx}
\usepackage{scrextend}
\usepackage{lastpage}
\usepackage{refcount}
\newif\ifAddOMR
\def\AddOMR{\AddOMRtrue}
\def\OmitOMR{\AddOMRfalse}
\newif\ifDoubleSidedPrinting
\def\SingleSidedPrinting{\DoubleSidedPrintingfalse}
\def\DoubleSidedPrinting{\DoubleSidedPrintingtrue}
%\SingleSidedPrinting %%if I define SingleSidedPrinting (or DoubleSidedPrinting) on this line
\DoubleSidedPrinting %%the code is working
\AddOMR %%if I define SingleSidedPrinting (or DoubleSidedPrinting) on this line
%\OmitOMR %%the code is working
\makeatletter
\newcommand*{\LastOddCheck}{
\ifodd\value{page}
\if@filesw
\immediate\write\@auxout{
\gdef\string\LastOddPage{\the\value{page}}
}
\fi
\fi
}
\newcommand*{\LastOddPage}{0}
\ifAddOMR
\ClearShipoutPicture
\AddToShipoutPicture{
\ifDoubleSidedPrinting
\ifodd\c@page
\LastOddCheck
\ifnum\LastOddPage=\value{page}
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,489.5){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\else
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\fi
\else
\fi
\else
\ifnum\getpagerefnumber{LastPage}=\value{page}
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,489.5){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\else
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\fi
\fi
}
\makeatother
\else
\fi
\ifDoubleSidedPrinting
\AtEndDocument{\cleardoublepage}
\else
\fi
% This will be the end of the reusable preamble
%\SingleSidedPrinting %%I would like to define SingleSidedPrinting (or DoubleSidedPrinting) only at this moment
%\DoubleSidedPrinting %%but if I do so, the code is not working. Is there a workaround?
\begin{document}
\newpage %% page 1
\mbox{Text on page 1}
\newpage %% page 2
\mbox{Text on page 2}
\newpage %% page 3
\mbox{Text on page 3}
\newpage %% page 4
\mbox{Text on page 4}
%\newpage %% page 5
%\mbox{Text on page 5}
\end{document}
答案1
您的\ifAddOMR
和\ifDoubleSidedPrinting
测试评估得太早了,因为您想在测试完成后使用\SingleSidedPrinting
、\DoubleSidedPrinting
或。解决方案是通过将测试+装饰代码放在展开的宏中来延迟对标志的测试\AddOMR
\OmitOMR
后你可以像这样设置标志:
\documentclass[12pt,twoside,a4paper]{article}
\usepackage{eso-pic}
\usepackage{rotating}
\usepackage{graphicx}
\usepackage{scrextend}
\usepackage{lastpage}
\usepackage{refcount}
\newif\ifAddOMR
\def\AddOMR{\AddOMRtrue}
\def\OmitOMR{\AddOMRfalse}
\newif\ifDoubleSidedPrinting
\def\SingleSidedPrinting{\DoubleSidedPrintingfalse}
\def\DoubleSidedPrinting{\DoubleSidedPrintingtrue}
\makeatletter
\newcommand*{\LastOddCheck}{
\ifodd\value{page}
\if@filesw
\immediate\write\@auxout{
\gdef\string\LastOddPage{\the\value{page}}
}
\fi
\fi
}
\newcommand*{\LastOddPage}{0}
\newcommand*{\myLayoutDependentAdditions}{%
\ifAddOMR
\ClearShipoutPicture
\AddToShipoutPicture{
\ifDoubleSidedPrinting
\ifodd\c@page
\LastOddCheck
\ifnum\LastOddPage=\value{page}
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,489.5){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\else
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\fi
\else
\fi
\else
\ifnum\getpagerefnumber{LastPage}=\value{page}
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,489.5){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\else
\put(11,503){\linethickness{0.5mm}{\line(1,0){17}}}
\put(11,475){\linethickness{0.5mm}{\line(1,0){17}}}
\fi
\fi
}
\else
\fi
\ifDoubleSidedPrinting
\AtEndDocument{\cleardoublepage}
\else
\fi
}
\makeatother
% This will be the end of the reusable preamble
\SingleSidedPrinting
\AddOMR
\myLayoutDependentAdditions
\begin{document}
\newpage %% page 1
\mbox{Text on page 1}
\newpage %% page 2
\mbox{Text on page 2}
\newpage %% page 3
\mbox{Text on page 3}
\newpage %% page 4
\mbox{Text on page 4}
%\newpage %% page 5
%\mbox{Text on page 5}
\end{document}
还请注意,我将其\makeatother
向下移动,因为我们想无条件地执行此操作(在所有情况下,无论设置了什么标志)。