% The UCW Macro Collection (a successor of mjmac.tex)
-% Written by Martin Mares <mj@ucw.cz> in 2010 and placed into public domain
-% -------------------------------------------------------------------------
+% Written by Martin Mares <mj@ucw.cz> in 2010--2018 and placed into public domain
+% -------------------------------------------------------------------------------
+
+\ifx\ucwmodule\undefined\else\endinput\fi
%%% Prolog %%%
% We'll use internal macros of plain TeX
\catcode`@=11
+\ifx\eTeXversion\undefined
+\errmessage{ucwmac requires the e-TeX engine or its successor}
+\fi
+
%%% PDF output detection %%%
\newif\ifpdf
\ifx\pdfoutput\undefined
\else\ifnum\pdfoutput>0
\pdftrue
+ \pdfpkresolution=600 % Provide a reasonable default
\fi\fi
+%%% Auxiliary macros %%%
+
+% Prepend/append #2 to the definition of #1
+\long\def\prependef#1#2{\expandafter\def\expandafter#1\expandafter{#2#1}}
+\long\def\appendef#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
+
+\def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
+
%%% Page size and margins %%%
% If you modify these registers, call \setuppage afterwards
-\newdimen\pagewidth
-\newdimen\pageheight
+\ifx\luatexversion\undefined
+ % In LuaTeX, \pagewidth and \pageheight are primitive
+ \newdimen\pagewidth
+ \newdimen\pageheight
+\fi
\newdimen\leftmargin
\newdimen\rightmargin
\newdimen\topmargin
\voffset=\topmargin
\advance\voffset by -1truein
\ifpdf
- \pdfpagewidth=\pagewidth
- \pdfpageheight=\pageheight
+ \pdfhorigin=1truein
+ \pdfvorigin=1truein
+ \ifx\luatexversion\undefined
+ \pdfpagewidth=\pagewidth
+ \pdfpageheight=\pageheight
+ \fi
\fi
}
\def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
% Define inner/outer margin instead of left/right
-\def\setinneroutermargin#1#2{\sethmargins{#1}{#2}\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
+\def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
% Use a predefined paper format, calls \setuppage automagically
\def\setpaper#1{%
\defpaper{a3}{297truemm}{420truemm}
\defpaper{a4}{210truemm}{297truemm}
\defpaper{a5}{148truemm}{210truemm}
-\defpaper{a6}{105truemm}{148truemm}
\defpaper{letter}{8.5truein}{11truein}
\defpaper{legal}{8.5truein}{14truein}
\setmargins{1truein}
\setpaper{a4}
+%%% Macros with optional arguments %%%
+
+% After \def\a{\withoptarg\b}, the macro \a behaves in this way:
+% \a[arg] does \def\optarg{arg} and then it expands \b
+% \a does \let\optarg=\relax and then it expands \b
+\def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
+\def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
+\def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
+
+% A shortcut for defining macros with optional arguments:
+% \optdef\macro behaves as \def\domacro, while \macro itself is defined
+% as a wrapper calling \domacro using \withoptarg.
+\def\optdef#1{%
+ \edef\xoptname{\expandafter\eatbackslash\string#1}%
+ \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
+ \expandafter\def\csname do\xoptname\endcsname
+}
+
+% Trick: \eatbackslash eats the next backslash of category 12
+\begingroup\lccode`\+=`\\
+\lowercase{\endgroup\def\eatbackslash+{}}
+
+% Expand to the optional argument if it exists
+\def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
+
%%% Placing material at specified coordinates %%%
% Set all dimensions of a given box register to zero
\def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
-\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
-\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
+\long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
+\long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
% Variants of \llap and \rlap working equally on both sides and/or vertically
\def\hlap#1{\hbox to 0pt{\hss #1\hss}}
\long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
% Like \vbox, but with reference point in the upper left corner
-\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
+\long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
% Like \vhang, but respecting interline skips
-\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
+\long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
% Crosshair with reference point in its center
\def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
% and underlays, which are zero-size vboxes positioned absolutely in the
% front / in the back of the normal material. Also, there are global
% versions of both which are not reset after every page.
-\def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\smashedvbox{#2}}}
+\def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
\def\pageunderlay{\addlay\pageunderlays}
\def\pageoverlay{\addlay\pageoverlays}
\def\commonunderlay{\addlay\commonoverlays}
% Our variation on \plainoutput, which manages inner/outer margins and overlays
\output{\ucwoutput}
+\newdimen\pagebodydepth
\def\ucwoutput{\wigglepage\shipout\vbox{%
\makeheadline
\ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
\ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
\pagebody
- \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays}\fi
- \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays}\fi
+ \pagebodydepth=\prevdepth
+ \nointerlineskip
+ \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
+ \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
+ \prevdepth=\pagebodydepth
\makefootline
}\advancepageno
\ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
\def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
+% Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
+\let\footfont=\tenrm
+\footline={\hss\footfont\folio\hss}
+
%%% Itemization %%%
% Default dimensions of itemized lists
\def\nnorm{\the\itemcount}
\def\ndotted{\nnorm.}
\def\nparen{\nnorm)}
+\def\nparenp{(\nnorm)}
\def\nroman{\romannumeral\itemcount}
\def\nromanp{\nroman)}
\def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
\zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
\or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
-%%% Verbatim typesetting %%%
-
-% New \frenchspacing, which doesn't leave unwanted spaces in text.
-\def\frenchsp@cing{\sfcode`\.\@m \sfcode`\?\@m \sfcode`\!\@m%
-\sfcode`\:\@m \sfcode`\;\@m \sfcode`\,\@m}
-
-% Typesetting of one verbatim word: |word|
-
-% Set if spaces should be rendered as "bath-tub" glyphs
-\newif\ifshowspaces
-\showspacesfalse
-
-\def\make@ther#1{\catcode`#1=12\relax}
-\def\setverb@tim{\tt\frenchsp@cing\catcode`\`=13\catcode`\'=13\n@ligs%
-\let\do=\make@ther\dospecials\obeyspaces\showsp@ces}
-
-{\catcode`'=13
-\catcode``=13
-\gdef\n@ligs{\def`{\relax\lq}\def'{\relax\rq}}}
-
-{\obeyspaces
-\global\chardef\sp@ce=32
-\gdef\showsp@ces{\ifshowspaces\let =\sp@ce\fi}
-\global\let =\ }
-
-\catcode124=\active % Make | active
-\def\ttst@rt{\begingroup\setverb@tim\let|=\endgroup}
-\let|=\ttst@rt
-
-% Typesetting of verbatim blocks:
-% - the block is enclosed in balanced {}'s
-% - leading and trailing empty lines are ignored
-% - all other empty lines are converted to \verbemptylineskip's
-% - tabs are interpreted (one tab stop per \verbtabsize columns)
-% - \showspacestrue works here, too, but it does not apply to tabs
-
-\newskip\verbstartskip % vskip before \verbatim
-\newskip\verbendskip % vskip after \verbatim
-\newskip\verbinterlineskip % between adjacent non-empty lines
-\newskip\verbemptylineskip % instead of every empty line
-\newskip\verbleftskip % left and right margin
-\newskip\verbrightskip
-
-\verbstartskip=3pt plus 1pt minus 0.3pt
-\verbendskip=\verbstartskip
-\verbinterlineskip=0pt
-\verbemptylineskip=5pt plus 2pt
-\verbleftskip=0in
-\verbrightskip=0pt plus 1fil
-
-% Can be re-defined to customize the verbatim environment
-\def\verblocaldefs{}
-
-% Size of a single tab
-\newcount\verbtabsize
-\verbtabsize=8
-
-\newdimen\v@rbspace
-\newdimen\t@b
-\newdimen\t@bwidth
-
-% When a complete line is assembled in \box0, \verbship is called to add it
-% to the main vertical list. You can override it to get e.g. line numbering.
-\def\verbship{%
- \ifdim\wd0>0pt
- \ifdim\v@rbspace>0pt
- \penalty-100
- \vskip\v@rbspace
- \else
- \ifdim\v@rbspace=0pt
- \vskip\verbinterlineskip
- \fi
- \fi
- \line{\hskip\verbleftskip \box0 \hskip\verbrightskip}
- \v@rbspace=0pt
- \else
- \advance\v@rbspace by \verbemptylineskip
- \fi
-}
+%%% Miscellanea %%%
-\newcount\verbcnt
-\def\v@rbend{\par\egroup\endgroup\vskip\verbendskip}
-\def\v@rbl{\ifnum\verbcnt>0\{\fi\global\advance\verbcnt by 1\relax}
-\def\v@rbr{\ifnum\verbcnt>1\}\else\v@rbend\fi\global\advance\verbcnt by -1\relax}
-
-\def\v@rbparams{%
- \setverb@tim
- \raggedbottom
- \verbcnt=0
- \v@rbspace=-1000pt
- \catcode124=12
- \vskip\verbstartskip
- \let\par=\endb@x
- \obeylines
-}
+% {\I italic} with automatic italic correction
+\def\I{\it\aftergroup\/}
-\def\startb@x{\setbox0=\hbox\bgroup}
-\def\endb@x{\egroup\verbship\startb@x}
+% A breakable dash, to be repeated on the next line
+\def\={\discretionary{-}{-}{-}}
-{\catcode`\^^I=\active
-\gdef\setupt@bs{\catcode`\^^I=\active
-\setbox0=\hbox{\ }\t@bwidth=\wd0\multiply\t@bwidth by \verbtabsize
-\def^^I{\egroup\t@b=\wd0\divide\t@b by \t@bwidth%
-\multiply\t@b by \t@bwidth%
-\advance\t@b by \t@bwidth\advance\t@b by -\wd0\startb@x\box0\hbox to \t@b{}}%
-}}
+% Non-breakable identifiers
+\def\<#1>{\leavevmode\hbox{\I #1}}
-{
-\catcode`[=1\catcode`]=2\catcode123=\active\catcode125=\active
-\gdef\verbatim[\begingroup\v@rbparams\catcode123=\active\catcode125=\active%
-\let{=\v@rbl\let}=\v@rbr\chardef\{=123\chardef\}=125\verblocaldefs\setupt@bs\startb@x%
-]]
+% Handy shortcuts
+\let\>=\noindent
+\def\\{\hfil\break}
-% Input file verbatim
+% Variants of \centerline, \leftline and \rightline, which are compatible with
+% verbatim environments and other catcode hacks
+\def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
+\def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
+\def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
-\def\verbinput#1{\begingroup\v@rbparams\verblocaldefs\setupt@bs\startb@x\input #1 \egroup\endgroup}
+% Insert a PDF picture
+% \putimage{width specification}{file}
+\def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
+
+% Let ~ be protected
+\let\plaintilde=~
+\protected\def~{\plaintilde}
+
+%%% Localization %%%
+
+% Current language
+\def\localelang{en}
+
+% Define a new localized string: \localedef{language}{identifier}{message}
+\def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
+
+% Expand a localized string in the current language: \localemsg{identifier}
+\def\localestr#1{%
+ \ifcsname loc:\localelang:#1\endcsname
+ \csname loc:\localelang:#1\endcsname
+ \else
+ \ucwwarn{Localized string #1 not defined in language \localelang}%
+ ???%
+ \fi
+}
+
+%%% Modules %%%
+
+\def\ucwmodule#1{
+ \ifcsname ucwmod:#1\endcsname
+ \else
+ \input ucw-#1.tex
+ \fi
+}
+
+\def\ucwdefmodule#1{
+ \ifcsname ucwmod:#1\endcsname\endinput\fi
+ \expandafter\let\csname ucwmod:#1\endcsname=\relax
+}
%%% Epilog %%%