# Define variables in document so that macro definition goes into preamble

I've written some macros to add OMR characters to the generated document. The OMR characters are needed for an envelope inserter machine.

When I want to print OMR characters for a double-sided document, I have to put:

 \DoubleSidedPrinting


I want to put my macro in a preamble that will be precompiled and be reused for several documents. In that case, I will only have to set DoubleSidedPrinting and AddOMR in each document. But this doesn't work. Do you know a workaround?

% 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\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}

\ClearShipoutPicture
\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}

• I'm not sure quite what you're asking. Can you provide an example of what you got, compared to what you were expecting? – Andy Clifton Apr 20 at 6:07
• The goal is to put the code between \documentclass and \begin{document} in a preamble that will be reused among several tex files. But in that case I won't be able to define the variables before I'm using them in my code. – user3048741 Apr 20 at 6:11

Your \ifAddOMR and \ifDoubleSidedPrinting tests are evaluated too early, since you want to use \SingleSidedPrinting, \DoubleSidedPrinting, \AddOMR or \OmitOMR after the tests have been performed. The solution is to delay testing of the flags by putting the tests+decoration code inside a macro that is expanded after you set the flags, like this:

\documentclass[12pt,twoside,a4paper]{article}

\usepackage{eso-pic}
\usepackage{rotating}
\usepackage{graphicx}
\usepackage{scrextend}
\usepackage{lastpage}
\usepackage{refcount}

\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}

\ClearShipoutPicture
\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

\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}


Also note that I moved the \makeatother down, because we want to do it unconditionally (in all cases, whatever flags are set).