| [2c052c0] | 1 | % Package for CFA Research Lab.
 | 
|---|
| [9d7e5cb] | 2 | % (Now more a personal collection and testing grounds for common.sty.)
 | 
|---|
| [2c052c0] | 3 | %
 | 
|---|
| [a049412] | 4 | % This is a collection of commands everyone working on CFA related documents
 | 
|---|
 | 5 | % should find useful. So mostly programming language related tools.
 | 
|---|
| [c72ea7a] | 6 | %
 | 
|---|
 | 7 | % Internal commands are prefixed with "\cfalab@".
 | 
|---|
| [2c052c0] | 8 | 
 | 
|---|
 | 9 | % I don't know what the oldest LaTeX2e version with everything needed is.
 | 
|---|
 | 10 | \NeedsTeXFormat{LaTeX2e}
 | 
|---|
| [c72ea7a] | 11 | \ProvidesPackage{cfalab}[2020/03/24 v0.1 CFA Laboratory LaTeX Tools]
 | 
|---|
| [2c052c0] | 12 | 
 | 
|---|
 | 13 | % Other packages required.
 | 
|---|
| [df24d37] | 14 | %
 | 
|---|
 | 15 | % Access to new basic LaTeX tools and other low level commands.
 | 
|---|
| [2c052c0] | 16 | \RequirePackage{etoolbox}
 | 
|---|
| [df24d37] | 17 | % Code formatting tools and environments.
 | 
|---|
| [c72ea7a] | 18 | \RequirePackage{listings}
 | 
|---|
| [df24d37] | 19 | % Automatically adds spaces.
 | 
|---|
| [2c052c0] | 20 | \RequirePackage{xspace}
 | 
|---|
 | 21 | 
 | 
|---|
| [a049412] | 22 | % Tip for commands that end with \xspace: if the default is not correct then
 | 
|---|
 | 23 | % follow the command with {} to disable \xspace, use '{} ' to force add a
 | 
|---|
 | 24 | % space and '{}<whatever-follows>' to force remove one.
 | 
|---|
 | 25 | %
 | 
|---|
| [8cd34e9] | 26 | % \CFA
 | 
|---|
| [2c052c0] | 27 | % Cforall with the forall symbol.
 | 
|---|
| [a049412] | 28 | \newrobustcmd\CFA{\textsf{C\raisebox{\depth}{\rotatebox{180}{A}}}\xspace}
 | 
|---|
| [8cd34e9] | 29 | % \Cpp[<std>]
 | 
|---|
 | 30 | % C++ symbol name. You may optionally provide <std> to specify a standard.
 | 
|---|
| [a049412] | 31 | \newrobustcmd\Cpp[1][\xspace]{C++#1}
 | 
|---|
| [6e7b969] | 32 | 
 | 
|---|
| [df24d37] | 33 | % This is executed very early in the \begin{document} code, before the
 | 
|---|
 | 34 | % document's contents but after packages are loaded.
 | 
|---|
| [6e7b969] | 35 | \AtEndPreamble{
 | 
|---|
 | 36 |   \@ifpackageloaded{hyperref}{
 | 
|---|
 | 37 |     % Convert symbols to pdf compatable forms when required.
 | 
|---|
 | 38 |     \pdfstringdefDisableCommands{
 | 
|---|
 | 39 |       \def\CFA{CFA}
 | 
|---|
| [df24d37] | 40 |       \def\Cpp{C++}
 | 
|---|
 | 41 |       \def\lstinline{}
 | 
|---|
| [c21f5a9] | 42 |       \def\code#1#2{#2}
 | 
|---|
| [6e7b969] | 43 |     }
 | 
|---|
 | 44 |   }{}
 | 
|---|
 | 45 | }
 | 
|---|
| [2c052c0] | 46 | 
 | 
|---|
| [df24d37] | 47 | % \colour{<colour>}{<text>}
 | 
|---|
| [edc6ea2] | 48 | % Just \color but using the LaTeX style instead of TeX style command.
 | 
|---|
| [df24d37] | 49 | \newcommand*\colour[2]{{\color{#1}#2}}
 | 
|---|
 | 50 | 
 | 
|---|
| [9d7e5cb] | 51 | % \code{<language>}{<code>}
 | 
|---|
 | 52 | % Use the listings package to format the snipit of <code> in <language>.
 | 
|---|
 | 53 | \newrobustcmd*\code[2]{\lstinline[language=#1]{#2}}
 | 
|---|
| [df24d37] | 54 | 
 | 
|---|
| [8cd34e9] | 55 | % \begin{cfa}[<options>]
 | 
|---|
 | 56 | % \end{cfa}
 | 
|---|
| [edc6ea2] | 57 | % Use the listings package to format a block of CFA code.
 | 
|---|
 | 58 | % Extra listings options can be passed in as an optional argument.
 | 
|---|
| [df24d37] | 59 | \lstnewenvironment{cfa}[1][]{\lstset{language=CFA}\lstset{#1}}{}
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | % \settextunderscore{(new|old)}
 | 
|---|
 | 62 | % Redefines the underscore either as a new repersentation or the old one.
 | 
|---|
 | 63 | % Not that some other packages (ex. hyperref) can override this. Set it up
 | 
|---|
 | 64 | % after loading them.
 | 
|---|
 | 65 | \let\cfalab@textunderscore@old=\textunderscore
 | 
|---|
 | 66 | \newcommand\cfalab@textunderscore@new{%
 | 
|---|
 | 67 |     \leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
 | 
|---|
 | 68 | \newcommand\settextunderscore[1]{%
 | 
|---|
 | 69 |     \renewcommand\textunderscore{\csuse{cfalab@textunderscore@#1}}}
 | 
|---|
 | 70 | 
 | 
|---|
| [c72ea7a] | 71 | % The CFA listings language. Based off of ANCI C and including GCC extensions.
 | 
|---|
 | 72 | % The UW dialect is the default (and only so far) dialect of the language.
 | 
|---|
 | 73 | \lstdefinelanguage[UW]{CFA}[ANSI]{C}{
 | 
|---|
 | 74 |     morekeywords={_Alignas, _Alignof, __alignof, __alignof__, asm, __asm,
 | 
|---|
 | 75 |         __asm__, __attribute, __attribute__, auto, _Bool, catch, catchResume,
 | 
|---|
 | 76 |         choose, _Complex, __complex, __complex__, __const, __const__,
 | 
|---|
 | 77 |         coroutine, disable, dtype, enable, exception, __extension__,
 | 
|---|
 | 78 |         fallthrough, fallthru, finally, __float80, float80, __float128,
 | 
|---|
 | 79 |         float128, forall, ftype, generator, _Generic, _Imaginary, __imag,
 | 
|---|
 | 80 |         __imag__, inline, __inline, __inline__, __int128, int128, __label__,
 | 
|---|
 | 81 |         monitor, mutex, _Noreturn, one_t, or, otype, restrict, resume,
 | 
|---|
 | 82 |         __restrict, __restrict__, __signed, __signed__, _Static_assert,
 | 
|---|
 | 83 |         suspend, thread, _Thread_local, throw, throwResume, timeout, trait,
 | 
|---|
 | 84 |         try, ttype, typeof, __typeof, __typeof__, virtual, __volatile,
 | 
|---|
 | 85 |         __volatile__, waitfor, when, with, zero_t
 | 
|---|
 | 86 |     },
 | 
|---|
 | 87 |     moredirectives={defined,include_next},
 | 
|---|
 | 88 | }
 | 
|---|
 | 89 | \lstset{defaultdialect={[UW]CFA}}
 | 
|---|
 | 90 | 
 | 
|---|
| [c1136c56] | 91 | % Create an internal paragraph indent amount. This is used internally to
 | 
|---|
 | 92 | % mimic the standard indent even when it has been overriden in the document.
 | 
|---|
 | 93 | \newlength\cfalab@parindent
 | 
|---|
 | 94 | \deflength\cfalab@parindent{\parindent}
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | % The cfacommon style has many useful defaults for CFA and other types of
 | 
|---|
 | 97 | % code. Use the listings option "style=cfacommon" to load them.
 | 
|---|
 | 98 | \lstdefinestyle{cfacommon}{
 | 
|---|
 | 99 |   columns=fullflexible,
 | 
|---|
 | 100 |   basicstyle=\linespread{0.9}\sf,
 | 
|---|
 | 101 |   stringstyle=\tt,
 | 
|---|
 | 102 |   tabsize=5,
 | 
|---|
 | 103 |   % Indent code to paragraph indentation.
 | 
|---|
 | 104 |   xleftmargin=\cfalab@parindent,
 | 
|---|
 | 105 |   % Allow ASCII characters in the range 128-255.
 | 
|---|
 | 106 |   extendedchars=true,
 | 
|---|
 | 107 |   % This allows you to use "math mode" to insert LaTeX into the code.
 | 
|---|
 | 108 |   % Use \( and \) if you need to insert math mode inside that code.
 | 
|---|
 | 109 |   escapechar=\$,
 | 
|---|
 | 110 |   % Disable LaTeX math escape in CFA code $...$
 | 
|---|
 | 111 |   mathescape=false,
 | 
|---|
 | 112 |   keepspaces=true,
 | 
|---|
 | 113 |   % Do not show spaces with cup.
 | 
|---|
 | 114 |   showstringspaces=false,
 | 
|---|
 | 115 |   % Show blank lines at end of code.
 | 
|---|
 | 116 |   showlines=true,
 | 
|---|
 | 117 |   % Spacing above/below code block.
 | 
|---|
 | 118 |   aboveskip=4pt,belowskip=0pt,
 | 
|---|
 | 119 |   numberstyle=\footnotesize\sf,
 | 
|---|
 | 120 |   % Replace/adjust listing characters that look bad in sanserif.
 | 
|---|
| [ec1c674] | 121 |   literate={-}{\makebox[1ex][c]{\raisebox{0.7ex}{\rule{0.75ex}{0.1ex}}}}1
 | 
|---|
| [c1136c56] | 122 |     {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1
 | 
|---|
 | 123 |     {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 {`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
 | 
|---|
 | 124 |     {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2
 | 
|---|
 | 125 |     {->}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex\textgreater}2,
 | 
|---|
| [c72ea7a] | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|
| [887fc79] | 128 | % \snake{<identifier>}
 | 
|---|
 | 129 | % Improves writing of snake case (or any convention that uses _) by allowing
 | 
|---|
 | 130 | % line breaks after _. Disables commands inside the block and formats the
 | 
|---|
 | 131 | % identifier to look like code.
 | 
|---|
 | 132 | \newcommand*\snake[1]{\snakefont{\expandafter\snake@next\detokenize{#1}\@nil}}
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 | % \snakefont{<text>}
 | 
|---|
 | 135 | % Command used by \snake, you may renew the command to change its formating.
 | 
|---|
 | 136 | \newcommand*\snakefont[1]{\texttt{#1}}
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | % Thanks Manuel of TeX Stack exchange. (I got the base pattern from one of
 | 
|---|
 | 139 | % their answers.) Note: \@nil should never be defined.
 | 
|---|
 | 140 | \newcommand*\snake@next[1]{\ifx\@nil#1\else
 | 
|---|
 | 141 |   \expandafter\ifx\string_#1\string_\allowbreak\else#1\fi
 | 
|---|
 | 142 |   \expandafter\snake@next\fi
 | 
|---|
 | 143 | }
 | 
|---|
 | 144 | 
 | 
|---|
| [d02e547] | 145 | % These somehow control how much of a page can be a floating element before
 | 
|---|
 | 146 | % the float is forced onto its own page.   
 | 
|---|
 | 147 | \renewcommand{\topfraction}{0.8}
 | 
|---|
 | 148 | \renewcommand{\bottomfraction}{0.8}
 | 
|---|
 | 149 | \renewcommand{\floatpagefraction}{0.8}
 | 
|---|
 | 150 | % Sort of the reverse, I think it is the minimum amount of text that can
 | 
|---|
 | 151 | % be on a page before its all removed. (0 for always fix what you can.)
 | 
|---|
 | 152 | \renewcommand{\textfraction}{0.0}
 | 
|---|
 | 153 | 
 | 
|---|
| [df24d37] | 154 | % common.tex Compatablity ===================================================
 | 
|---|
 | 155 | % Below this line is for compatability with the old common.tex file.
 | 
|---|
| [c72ea7a] | 156 | 
 | 
|---|
| [c1136c56] | 157 | % Backwards compatable way to activate the cfacommon style.
 | 
|---|
 | 158 | \newcommand{\CFAStyle}{\lstset{style=cfacommon}}
 | 
|---|
| [df24d37] | 159 | 
 | 
|---|
| [edc6ea2] | 160 | % A couple of abbreviations are provided. Just ones someone liked.
 | 
|---|
 | 161 | %
 | 
|---|
 | 162 | % Abbreviation formatting commands (renew to customize):
 | 
|---|
 | 163 | \newcommand{\abbrevFont}{\textit}
 | 
|---|
 | 164 | %
 | 
|---|
 | 165 | % Abbreviations that, if not followed by a comma or colon, add a comma.
 | 
|---|
 | 166 | \newrobustcmd*\cfalab@abbrev@comma{%
 | 
|---|
 | 167 |   \@ifnextchar{,}{}{\@ifnextchar{:}{}{,\xspace}}}
 | 
|---|
 | 168 | \providerobustcmd*\eg{\abbrevFont{e}.\abbrevFont{g}.\cfalab@abbrev@comma}
 | 
|---|
 | 169 | \providerobustcmd*\ie{\abbrevFont{i}.\abbrevFont{e}.\cfalab@abbrev@comma}
 | 
|---|
 | 170 | %
 | 
|---|
 | 171 | % Abbreviations that, if not followed by a period, add a period.
 | 
|---|
 | 172 | \newrobustcmd*\cfalab@abbrev@period{\@ifnextchar{.}{}{.\xspace}}
 | 
|---|
 | 173 | \providerobustcmd*\etc{\abbrevFont{etc}\cfalab@abbrev@period}
 | 
|---|
 | 174 | \providerobustcmd*\etal{\abbrevFont{et}~\abbrevFont{al}\cfalab@abbrev@period}
 | 
|---|
 | 175 | \providerobustcmd*\viz{\abbrevFont{viz}\cfalab@abbrev@period}
 | 
|---|
| [2c052c0] | 176 | 
 | 
|---|
 | 177 | \endinput
 | 
|---|