source: doc/LaTeXmacros/common.tex@ a17f496

Last change on this file since a17f496 was b22b28e, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

new checking for empty optional parameters in latex macros

  • Property mode set to 100644
File size: 16.0 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%%
3%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4%%
5%% The contents of this file are covered under the licence agreement in the
6%% file "LICENCE" distributed with Cforall.
7%%
8%% common.tex --
9%%
10%% Author : Peter A. Buhr
11%% Created On : Sat Apr 9 10:06:17 2016
12%% Last Modified By : Peter A. Buhr
13%% Last Modified On : Mon May 5 21:34:53 2025
14%% Update Count : 709
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17% This latex idiom for checking empty optional parameters
18% \ifx#1\@empty\else\if\relax\detokenize{#1}\relax
19% first checks if there is no optional parameter specified: \name{...} versus \name[]{...}
20% second checks if the optional parameter is specified but empty: \name[]{...} versus \name[...]{...}
21
22\setlength{\textheight}{9in}
23%\oddsidemargin 0.0in
24\renewcommand{\topfraction}{0.8} % float must be greater than X of the page before it is forced onto its own page
25\renewcommand{\bottomfraction}{0.8} % float must be greater than X of the page before it is forced onto its own page
26\renewcommand{\floatpagefraction}{0.8} % float must be greater than X of the page before it is forced onto its own page
27\renewcommand{\textfraction}{0.0} % the entire page maybe devoted to floats with no text on the page at all
28
29\lefthyphenmin=3 % hyphen only after 4 characters
30\righthyphenmin=3
31
32\usepackage[ignoredisplayed]{enumitem} % do not affect trivlist
33\setlist{labelsep=1ex}% global
34\setlist{topsep=0pt}% global
35\setlist[itemize]{parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent}% global
36\setlist[itemize,1]{label=\textbullet}% local
37%\renewcommand{\labelitemi}{{\raisebox{0.25ex}{\footnotesize$\bullet$}}}
38\setlist[enumerate]{parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent}% global
39\setlist[enumerate,2]{labelsep=*,align=parleft}% local
40\setlist[description]{itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}
41
42% Names used in the document.
43
44\usepackage{xspace}
45\newcommand{\CFAIcon}{\textsf{C\raisebox{\depth}{\rotatebox{180}A}}} % Cforall icon
46\newcommand{\CFA}{\protect\CFAIcon\xspace} % CFA symbolic name
47\newcommand{\CFL}{\textrm{Cforall}\xspace} % Cforall non-icon name
48\newcommand{\Celeven}{\textrm{C1\hspace*{-0.4ex}1}\xspace} % C11 symbolic name
49
50\newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon
51% numbers disallowed in latex variables names => use number names
52\newcommand{\CCeleven}{\protect\CCIcon{1\hspace*{-0.4ex}1}\xspace} % C++11 symbolic name
53\newcommand{\CCfourteen}{\protect\CCIcon{14}\xspace} % C++14 symbolic name
54\newcommand{\CCseventeen}{\protect\CCIcon{17}\xspace} % C++17 symbolic name
55\newcommand{\CCtwenty}{\protect\CCIcon{20}\xspace} % C++20 symbolic name
56\newcommand{\CC}[1][]{\protect\CCIcon{#1}\xspace} % C++ symbolic name
57\newcommand{\Cpp}[1][]{\CC{#1}} % C++ synonym
58
59\newcommand{\Csharp}{C\raisebox{-0.7ex}{\relsize{2}$^\sharp$}\xspace} % C# symbolic name
60
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
63\usepackage[scaled=0.85]{helvet} % descent Helvetica font and scale to times size
64\usepackage[T1]{fontenc}
65\usepackage{relsize} % must be after change to small or selects old size
66\usepackage{rotating}
67\usepackage{calc} % latex arithmetic
68
69% remove special-character warning in PDF side-bar names
70\makeatletter
71\@ifpackageloaded{hyperref}{
72 \pdfstringdefDisableCommands{
73 \def\CFA{\CFL}
74 \def\Celeven{C11\xspace}
75 \def\CC{C++\xspace}
76 \def\CCeleven{C++11\xspace}
77 \def\CCfourteen{C++14\xspace}
78 \def\CCseventeen{C++17\xspace}
79 \def\CCtwenty{C++20\xspace}
80 \def\Csharp{C\#\xspace}
81 \def\lstinline{\xspace}% must use {} as delimiters, e.g., \lstinline{...}
82 }{}
83}
84
85% parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
86% use rather than use \parident directly.
87\newlength{\parindentlnth}
88\setlength{\parindentlnth}{\parindent}
89
90% reduce size of chapter/section titles
91\def\@makechapterhead#1{%
92 \vspace*{50\p@}%
93 {\parindent \z@ \raggedright \normalfont
94 \ifnum \c@secnumdepth >\m@ne
95 \large\bfseries \@chapapp\space \thechapter
96 \par\nobreak
97 \vskip 5\p@
98 \fi
99 \interlinepenalty\@M
100 \Large \bfseries #1\par\nobreak
101 \vskip 50\p@
102 }}
103\def\@makeschapterhead#1{%
104 \vspace*{50\p@}%
105 {\parindent \z@ \raggedright
106 \normalfont
107 \interlinepenalty\@M
108 \Large \bfseries #1\par\nobreak
109 \vskip 50\p@
110 }}
111\renewcommand\section{\@startsection{section}{1}{\z@}{-3.0ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\large\bfseries}}
112\renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-2.75ex \@plus -1ex \@minus -.2ex}{1.25ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
113\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
114\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}}
115\renewcommand\subparagraph{\@startsection{subparagraph}{4}{\z@}{-1.5ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries\itshape}}
116
117\newcommand{\italic}[1]{\emph{\hyperpage{#1}}}
118\newcommand{\Definition}[1]{\textbf{\hyperpage{#1}}}
119\newcommand{\see}{\protect\@ifstar\@ssee\@see}
120\newcommand{\@ssee}[1]{(See #1)}
121\newcommand{\@see}[1]{(see #1)}
122
123% index macros
124
125% Define some commands that produce formatted index entries suitable for cross-references.
126% ``\spec'' produces entries for specifications of entities. ``\impl'' produces entries for their
127% implementations, and ``\use'' for their uses.
128
129% \newcommand{\bold}[1]{{\bf #1}}
130% \def\spec{\@bsphack\begingroup
131% \def\protect##1{\string##1\space}\@sanitize
132% \@wrxref{|bold}}
133\def\impl{\@bsphack\begingroup
134 \def\protect##1{\string##1\space}\@sanitize
135 \@wrxref{|Definition}}
136\newcommand{\indexcode}[1]{{\lstinline$#1$}}
137\def\use{\@bsphack\begingroup
138 \def\protect##1{\string##1\space}\@sanitize
139 \@wrxref{|hyperpage}}
140\def\@wrxref#1#2{\let\thepage\relax
141 \xdef\@gtempa{\write\@indexfile{\string
142 \indexentry{#2@{\lstinline$#2$}#1}{\thepage}}}\endgroup\@gtempa
143 \if@nobreak \ifvmode\nobreak\fi\fi\@esphack}
144%\newcommand{\use}[1]{\index{#1@{\lstinline$#1$}}}
145%\newcommand{\impl}[1]{\index{\protect#1@{\lstinline$\protect#1$}|Definition}}
146
147% inline text and lowercase index: \Index{inline and lowercase index text}
148\newcommand{\Index}{\@ifstar\@sIndex\@Index}
149% inline text and as-in index: \Index[as-is index text]{inline text}
150\newcommand{\@Index}[2][\@empty]{\lowercase{\def\temp{#2}}#2\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\index{\temp}\else\index{#1@{\protect#2}}\fi\fi}
151% inline text but index with different as-is text: \Index[index text]{inline text}
152\newcommand{\@sIndex}[2][\@empty]{#2\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\index{#2}\else\index{#1@{\protect#2}}\fi\fi}
153
154% inline text and code index (cannot use ©)
155\newcommand{\Indexc}[2][\@empty]{\lstinline[#1]$#2$\index{#2@\lstinline[#1]$#2$}}
156% code index (cannot use ©)
157\newcommand{\indexc}[2][\@empty]{\index{#2@\lstinline[#1]$#2$}}
158
159% Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}.
160% The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc}
161% The star version does not lowercase the index information, e.g., \newterm*{IBM}.
162\newcommand{\newtermFontInline}{\emph}
163\newcommand{\newterm}{\protect\@ifstar\@snewterm\@newterm}
164\newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\index{#2}\else\index{#1@{\protect#2}}\fi\fi}
165\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\index{\temp}\else\index{#1@{\protect#2}}\fi\fi}
166
167% \snake{<identifier>}
168% Improves writing of snake case (or any convention that uses _) by allowing
169% line breaks after _. Disables commands inside the block and formats the
170% identifier to look like code.
171\newcommand*\snake[1]{\snakefont{\expandafter\snake@next\detokenize{#1}\@nil}}
172
173% \snakefont{<text>}
174% Command used by \snake, you may renew the command to change its formating.
175\newcommand*\snakefont[1]{\LstBasicStyle{#1}}
176
177% Thanks Manuel of TeX Stack exchange. (I got the base pattern from one of
178% their answers.) Note: \@nil should never be defined.
179\newcommand*\snake@next[1]{\ifx\@nil#1\else
180 \expandafter\ifx\string_#1\string_\allowbreak\else#1\fi
181 \expandafter\snake@next\fi
182}
183
184% \lang{<language>}{<code>}
185% Use the listings package to format the snipit of <code> in <language>.
186\newcommand{\lang}[2]{\lstinline[language=#1]{#2}}
187
188% Latin abbreviation
189\newcommand{\abbrevFont}{\textit} % set empty for no italics
190% If not followed by a comma or colon, add a comma.
191\newcommand{\CheckCommaColon}{\@ifnextchar{,}{}{\@ifnextchar{:}{}{,\xspace}}}
192% If not followed by a period, add a period.
193\newcommand{\CheckPeriod}{\@ifnextchar{.}{}{.\xspace}}
194
195\newcommand{\EG}{\abbrevFont{e}.\abbrevFont{g}.}
196\newcommand{\eg}{\EG\protect\CheckCommaColon}
197
198\newcommand{\IE}{\abbrevFont{i}.\abbrevFont{e}.}
199\newcommand{\ie}{\IE\protect\CheckCommaColon}
200
201\newcommand{\ETC}{\abbrevFont{etc}}
202\newcommand{\etc}{\ETC\protect\CheckPeriod}
203
204\newcommand{\ETAL}{\abbrevFont{et}~\abbrevFont{al}}
205\newcommand{\etal}{\ETAL\protect\CheckPeriod}
206
207\newcommand{\VIZ}{\abbrevFont{viz}}
208\newcommand{\viz}{\VIZ\protect\CheckPeriod}
209
210\newcommand{\VS}{\abbrevFont{vs}}
211\newcommand{\vs}{\VS\protect\CheckPeriod}
212
213\newenvironment{cquote}{%
214 \list{}{\topsep=\lst@aboveskip\parskip=0pt\partopsep=0pt\itemsep=0pt\parsep=0pt\listparindent=0pt\leftmargin=\parindentlnth\rightmargin=0pt}%
215 \item\relax
216 \lstset{resetmargins=true}
217}{%
218 \endlist
219}% cquote
220\makeatother
221
222\newenvironment{rationale}{%
223 \begin{cquote}\noindent$\Box$\enspace
224}{%
225 \hfill\enspace$\Box$\end{cquote}
226}%
227
228% blocks and titles
229\newcommand{\define}[1]{\emph{#1\/}\index{#1}}
230\newcommand{\rewriterules}{\paragraph{Rewrite Rules}~\par\noindent}
231\newcommand{\examples}{\paragraph{Examples}~\par\noindent}
232\newcommand{\semantics}{\paragraph{Semantics}~\par\noindent}
233\newcommand{\constraints}{\paragraph{Constraints}~\par\noindent}
234\newcommand{\predefined}{\paragraph{Predefined Identifiers}~\par\noindent}
235
236% BNF macros
237\newenvironment{syntax}{
238\paragraph{Syntax}
239\begin{cquote}
240\begin{description}[noitemsep,leftmargin=\parindentlnth]
241}{
242\end{description}
243\end{cquote}
244}
245% \def\syntax{\paragraph{Syntax}\trivlist\parindent=.5in\item[\hskip.5in]}
246%\let\endsyntax=\endtrivlist
247\newcommand{\lhs}[1]{\item[\emph{#1:}\index{#1@{\emph{#1}}|italic}]~\ignorespaces}
248\newcommand{\oldlhs}[1]{\item[\emph{#1:}\index{#1@{\emph{#1}}|italic}~\dots]~}
249\newcommand{\rhs}{\hfil\newline}
250\newcommand{\nonterm}[1]{\emph{#1}\index{#1@{\emph{#1}}|italic}}
251\newcommand{\opt}{$_{opt}$\ }
252
253\usepackage{varioref} % extended references
254% adjust varioref package with default "section" and "page" titles, and optional title with faraway page numbers
255% \VRef{label} => Section 2.7, \VPageref{label} => page 17
256% \VRef[Figure]{label} => Figure 3.4, \VPageref{label} => page 17
257\renewcommand{\reftextfaceafter}{\unskip}
258\renewcommand{\reftextfacebefore}{\unskip}
259\renewcommand{\reftextafter}{\unskip}
260\renewcommand{\reftextbefore}{\unskip}
261\renewcommand{\reftextfaraway}[1]{\unskip, p.~\pageref{#1}}
262\renewcommand{\reftextpagerange}[2]{\unskip, pp.~\pageref{#1}--\pageref{#2}}
263\newcommand{\VRef}[2][Section]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else{#1}\nobreakspace\fi\fi\vref{#2}}
264\newcommand{\VRefrange}[3][Sections]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else{#1}\nobreakspace\fi\fi\vrefrange{#2}{#3}}
265\newcommand{\VPageref}[2][page]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else{#1}\nobreakspace\fi\fi\pageref{#2}}
266\newcommand{\VPagerefrange}[3][pages]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else{#1}\nobreakspace\fi\fi\pageref{#2}{#3}}
267
268\let\Oldthebibliography\thebibliography
269\renewcommand\thebibliography[1]{
270 \Oldthebibliography{#1}
271 \setlength{\parskip}{0pt} % reduce vertical spacing between references
272 \setlength{\itemsep}{5pt plus 0.3ex}
273}%
274
275\usepackage{listings} % format program code
276\usepackage{lstlang}
277
278\makeatletter
279\lst@AddToHook{OnEmptyLine}{\addtocounter{lstnumber}{-1}}% remove empty line number increment from listings
280
281\newcommand{\LstBasicStyle}[1]{{\lst@basicstyle{#1}}}
282\newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}}
283\newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}
284\newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}}
285\newcommand{\LstNumberStyle}[1]{{\lst@basicstyle{\lst@numberstyle{#1}}}}
286
287\newlength{\gcolumnposn} % temporary hack because lstlisting does not handle tabs correctly
288\newlength{\columnposn}
289\setlength{\gcolumnposn}{3in}
290\setlength{\columnposn}{\gcolumnposn}
291\newcommand{\setgcolumn}[1]{\global\gcolumnposn=#1\global\columnposn=\gcolumnposn}
292\newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstCommentStyle{#2}}}
293\newcommand{\CD}[2][\@empty]{\ifx#1\@empty\else\if\relax\detokenize{#1}\relax\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstBasicStyle{#2}}}
294\newcommand{\CRT}{\global\columnposn=\gcolumnposn}
295
296% allow escape sequence in lstinline
297%\usepackage{etoolbox}
298%\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}}
299
300% allow adding to lst literate
301\def\addToLiterate#1{\protect\edef\lst@literate{\unexpanded\expandafter{\lst@literate}\unexpanded{#1}}}
302\lst@Key{add to literate}{}{\addToLiterate{#1}}
303\makeatother
304
305\newcommand{\CFAStyle}{%
306\lstset{
307columns=fullflexible,
308basicstyle=\linespread{0.9}\sf, % reduce line spacing and use sanserif font
309stringstyle=\small\tt, % use typewriter font
310tabsize=5, % N space tabbing
311xleftmargin=\parindentlnth, % indent code to paragraph indentation
312extendedchars=true, % allow ASCII characters in the range 128-255
313mathescape=false, % disable LaTeX math escape in CFA code $...$
314keepspaces=true, %
315showstringspaces=false, % do not show spaces with cup
316showlines=true, % show blank lines at end of code
317aboveskip=4pt, % spacing above/below code block
318belowskip=2pt,
319numberstyle=\footnotesize\sf, % numbering style
320% replace/adjust listing characters that look bad in sanserif
321literate=
322% {-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.75ex}{0.1ex}}}}1
323 {-}{\raisebox{-1pt}{\ttfamily-}}1
324 {^}{\raisebox{0.6ex}{\(\scriptstyle\land\,\)}}1
325 {~}{\raisebox{0.3ex}{\(\scriptstyle\sim\,\)}}1
326% {'}{\ttfamily'\hspace*{-0.4ex}}1
327% {`}{\ttfamily\upshape\hspace*{-0.3ex}`}1
328 {`}{\raisebox{-2pt}{\large\textasciigrave\hspace{-1pt}}}1
329 {<-}{$\leftarrow$}2
330 {=>}{$\Rightarrow$}2
331% {->}{\raisebox{-1pt}{\texttt{-}}\kern-0.1ex\textgreater}2,
332}% lstset
333}% CFAStyle
334
335\ifdefined\CFALatin% extra Latin-1 escape characters
336\lstnewenvironment{cfa}[1][]{% necessary
337\lstset{
338language=CFA,
339escapechar=§, % LaTeX escape in CFA code §...§ (section symbol), emacs: C-q M-'
340moredelim=**[is][\color{red}]{®}{®}, % red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
341%moredelim=**[is][\color{blue}]{ß}{ß}, % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
342%moredelim=**[is][\color{OliveGreen}]{¢}{¢}, % green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
343%moredelim=[is][\lstset{keywords={}}]{¶}{¶}, % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
344}% lstset
345\lstset{#1}% necessary
346}{}
347% inline code ©...© (copyright symbol) emacs: C-q M-)
348\lstMakeShortInline© % single-character for \lstinline
349
350\else% regular ASCI characters
351
352\lstnewenvironment{cfa}[1][]{% necessary
353\lstset{
354language=CFA,
355escapechar=\$, % LaTeX escape in CFA code
356moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@
357}% lstset
358\lstset{#1}% necessary
359}{}
360% inline code @...@ (at symbol)
361\lstMakeShortInline@ % single-character for \lstinline
362\fi%
363
364\usepackage{tabularx} % if @ is used for lstMakeShortInline, allows @{}
365
366% Local Variables: %
367% tab-width: 4 %
368% fill-column: 100 %
369% End: %
Note: See TracBrowser for help on using the repository browser.