| 1 |
%%%============================================================================== |
| 2 |
% WinEdt pragmas |
| 3 |
% !Mode:: "TeX:EN" |
| 4 |
% Default Compile engines: |
| 5 |
% !TEX program = pdflatex |
| 6 |
% !PDFTeXify ext = --enable-etex --restrict-write18 |
| 7 |
% !PDFLaTeX ext = --enable-etex --restrict-write18 |
| 8 |
% !BIB program = biber |
| 9 |
%%%============================================================================== |
| 10 |
%% Copyright 2025-present by Alceu Frigeri |
| 11 |
%% |
| 12 |
%% This work may be distributed and/or modified under the conditions of |
| 13 |
%% |
| 14 |
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt), |
| 15 |
%% version 1.3c (or later), and/or |
| 16 |
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html), |
| 17 |
%% version 3 (or later) |
| 18 |
%% |
| 19 |
%% This work has the LPPL maintenance status *maintained*. |
| 20 |
%% |
| 21 |
%% The Current Maintainer of this work is Alceu Frigeri |
| 22 |
%% |
| 23 |
%% This is version {1.1b} {2025/10/28} |
| 24 |
%% |
| 25 |
%% The list of files that compose this work can be found in the README.md file at |
| 26 |
%% https://ctan.org/pkg/switch |
| 27 |
%% |
| 28 |
%%%============================================================================== |
| 29 |
\documentclass[10pt]{article} |
| 30 |
\RequirePackage[verbose,a4paper,marginparwidth=27.5mm,top=2.5cm,bottom=1.5cm,hmargin={40mm,20mm},marginparsep=2.5mm,columnsep=10mm,asymmetric]{geometry} |
| 31 |
\usepackage[T1]{fontenc} |
| 32 |
\usepackage[utf8]{inputenc} |
| 33 |
\usepackage{lmodern} |
| 34 |
\usepackage[infograb]{codedescribe} |
| 35 |
\RequirePackage[inline]{enumitem} |
| 36 |
\SetEnumitemKey{miditemsep}{parsep=0ex,itemsep=0.4ex} |
| 37 |
|
| 38 |
%%\usepackage[american,siunitx,cuteinductors,smartlabels,arrowmos,EFvoltages,betterproportions]{circuitikz} |
| 39 |
%%\usetikzlibrary{math} |
| 40 |
|
| 41 |
\usepackage{switch} |
| 42 |
|
| 43 |
\RequirePackage[backend=biber]{biblatex} |
| 44 |
\addbibresource{switch.bib} |
| 45 |
|
| 46 |
\RequirePackage[hidelinks,hypertexnames=false]{hyperref} |
| 47 |
|
| 48 |
|
| 49 |
\begin{document} |
| 50 |
\tstitle{ |
| 51 |
author={Alceu Frigeri\footnote{\tsverb{https://github.com/alceu-frigeri/switch}}}, |
| 52 |
date={\tsdate}, |
| 53 |
title={The switch Package\break Version \PkgInfo{switch}{version}} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
\begin{typesetabstract} |
| 58 |
|
| 59 |
This package offers two commands aimed at implementing a switch/case alike command. |
| 60 |
\end{typesetabstract} |
| 61 |
|
| 62 |
\tableofcontents |
| 63 |
|
| 64 |
\section{Introduction} |
| 65 |
There are many ways to implement a switch/case alike programming structure. Notably, one can use \tsobj{\str_case:nn} from \tsobj[pkg]{expl3}, or go over a loop using \tsobj{\pdfstrcmp}, or construct an if-then-else tower, etc. |
| 66 |
|
| 67 |
This implements a solution, based on \cite{stackexchage:switchcase}, which has the advantage, once the cases are set up, of being constant time: a single (\tsobj{\ifcsname}) is needed to select the correct code to be executed. |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
\begin{tsremark} |
| 72 |
The implementation creates a \tsobj{\csname} for each case, and it uses the primitive \tsobj{\ifcsname} to select the correct case. |
| 73 |
\end{tsremark} |
| 74 |
\begin{tsremark} |
| 75 |
The coding is done using \tsobj[pkg]{expl3}, just for the sake of readability, in the package comments one can find an implementation using just \TeX\ primitives. |
| 76 |
\end{tsremark} |
| 77 |
|
| 78 |
\setnewcodekey {switch} |
| 79 |
{ |
| 80 |
keywd2={newswitch,switch_new:Nn,addcase,switch_addcase:Nnn}, |
| 81 |
emph={TextCase,myCase}, |
| 82 |
emph2={CaseAstring}, |
| 83 |
codeprefix={}, |
| 84 |
resultprefix={} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
\section{Commands} |
| 89 |
Two set of commands are created, one to be used in a \tsobj[pkg]{expl3} code régime, and another set to be used in a user document. |
| 90 |
|
| 91 |
\subsection{User Document ones} |
| 92 |
\begin{codedescribe}{\newswitch} |
| 93 |
\begin{codesyntax}% |
| 94 |
\tsobj{\newswitch} \tsmeta{switch} \tsargs[marg]{default-code} |
| 95 |
\end{codesyntax} |
| 96 |
It will create a new switch \tsobj[marg]{switch}, which will expects a single argument. In case the argument doesn't corresponds to any defined case, \tsobj[marg]{default-code} will be used. The resulting \tsobj[marg]{switch} command is expandable, if \tsobj[marg]{default-code} and \tsobj[marg]{case-code} (added by \tsobj{\addcase}) also are. This is just an alias for \tsobj{\switch_new:Nn} |
| 97 |
\end{codedescribe} |
| 98 |
\begin{tsremark} |
| 99 |
\verb|#1| can be used in \tsobj[marg]{default-code}. An error is raised if \tsobj[marg]{switch} is already defined. |
| 100 |
\end{tsremark} |
| 101 |
|
| 102 |
\begin{codedescribe}{\addcase} |
| 103 |
\begin{codesyntax}% |
| 104 |
\tsobj{\addcase} \tsmeta{switch} \tsargs[marg]{case,case-code} |
| 105 |
\end{codesyntax} |
| 106 |
It will add a \tsobj[marg]{case} to a previously defined \tsmeta{switch} and associates \tsobj[marg]{case-code} with it. \tsobj[marg]{case} will be fully expanded at definition time. Once defined one can call \tsverb[verb]{\switch{case}}, which will put said \tsobj[marg]{case-code} in the input stream. This is just an alias for \tsobj{\switch_addcase:Nnn}. |
| 107 |
\end{codedescribe} |
| 108 |
|
| 109 |
\subsubsection{Example} |
| 110 |
|
| 111 |
First we create a switch, and associate a few (or more) cases. Note the possibility of using an auxiliary (fully expandable) macro/command when defining the cases. |
| 112 |
\begin{codestore}[switch02] |
| 113 |
\def\CaseAstring{case-A} |
| 114 |
\newswitch \myCase {I~ don't~ know:~ #1\par} |
| 115 |
\addcase \myCase {\CaseAstring} {A~ was~ used\par} |
| 116 |
\addcase \myCase {case-B} {B~ was~ used\par} |
| 117 |
\end{codestore} |
| 118 |
|
| 119 |
\begin{codestore}[switch02] |
| 120 |
\def\somemacro{case-A} |
| 121 |
\def\someothermacro{case-X} |
| 122 |
|
| 123 |
If B, then \myCase{case-B} |
| 124 |
If A, then \myCase{case-A} |
| 125 |
If X, then \myCase{case-X} |
| 126 |
|
| 127 |
if somemacro: \myCase{\somemacro} |
| 128 |
if someothermacro: \myCase{\someothermacro} |
| 129 |
\end{codestore} |
| 130 |
|
| 131 |
\tscode*[switch]{switch02}[1] |
| 132 |
\tsexec{switch02}[1] |
| 133 |
|
| 134 |
To use the \tsobj[marg]{switch}, one just has to call it with \tsobj[marg]{case} as an argument. Note the possibility of using an auxiliary macro/command (which has to be fully expandable) as a \tsobj[marg]{case}. |
| 135 |
|
| 136 |
\tsdemo[switch]{switch02}[2] |
| 137 |
|
| 138 |
|
| 139 |
\subsection{Expl3 ones} |
| 140 |
\begin{codedescribe}{\switch_new:Nn} |
| 141 |
\begin{codesyntax}% |
| 142 |
\tsobj{\switch_new:Nn} \tsmeta{switch} \tsargs[marg]{default-code} |
| 143 |
\end{codesyntax} |
| 144 |
It will create a new switch \tsobj[marg]{switch}, which will, in principle, expects a single, type n, argument. In case the argument doesn't corresponds to any defined case, \tsobj[marg]{default-code} will be used. The resulting \tsobj[marg]{switch} command is expandable, if \tsobj[marg]{default-code} and \tsobj[marg]{case-code} (added by \tsobj{\switch_addcase:Nnn}) also are. |
| 145 |
\end{codedescribe} |
| 146 |
\begin{tsremark} |
| 147 |
\verb|#1| can be used in \tsobj[marg]{default-code}. An error is raised if \tsobj[marg]{switch} is already defined. |
| 148 |
\end{tsremark} |
| 149 |
|
| 150 |
\begin{codedescribe}{\switch_addcase:Nnn} |
| 151 |
\begin{codesyntax}% |
| 152 |
\tsobj{\switch_addcase:Nnn} \tsmeta{switch} \tsargs[marg]{case,case-code} |
| 153 |
\end{codesyntax} |
| 154 |
It will add a \tsobj[marg]{case} to a previously defined \tsmeta{switch} and associates \tsobj[marg]{case-code} with it. \tsobj[marg]{case} will be fully expanded at definition time. Once defined one can call \tsverb[verb]{\switch{case}}, which will put said \tsobj[marg]{case-code} in the input stream. |
| 155 |
\end{codedescribe} |
| 156 |
|
| 157 |
\begin{codedescribe}[code,new=2025-05-13,EXP]{\switch_if_exist:NTF,\switch_if_case_exist:NnTF} |
| 158 |
\begin{codesyntax}% |
| 159 |
\tsobj{\switch_if_exist:NTF} \tsmeta{switch} \tsargs[marg]{if-true,if-false} |
| 160 |
\tsobj{\switch_if_case_exist:NnTF} \tsmeta{switch} \tsargs[marg]{case,if-true,if-false} |
| 161 |
\end{codesyntax} |
| 162 |
Tests if the \tsobj[marg]{switch}, or \tsobj[marg]{case}, are defined or not. It doesn't test if they are really a \tsobj[marg]{switch}/\tsobj[marg]{case}. |
| 163 |
\end{codedescribe} |
| 164 |
|
| 165 |
\begin{codedescribe}[code,new=2025-05-13]{\switch_undefine:N,\switch_case_undefine:Nn} |
| 166 |
\begin{codesyntax}% |
| 167 |
\tsobj{\switch_undefine:N} \tsmeta{switch} |
| 168 |
\tsobj{\switch_case_undefine:Nn} \tsmeta{switch} \tsargs[marg]{case} |
| 169 |
\end{codesyntax} |
| 170 |
Undefine the \tsobj[marg]{switch} and/or specific \tsobj[marg]{case}. Please note, when undefining a \tsobj[marg]{switch}, the \tsverb[verb]{\csname} associated with the cases aren't undefined (if needed, they have to be undefined one by one). |
| 171 |
\end{codedescribe} |
| 172 |
|
| 173 |
\subsubsection{Example} |
| 174 |
|
| 175 |
First we create a switch, and associate a few (or more) cases. Note the possibility of using an auxiliary (fully expandable) macro/command when defining the cases. |
| 176 |
\begin{codestore}[switch01] |
| 177 |
\ExplSyntaxOn |
| 178 |
\def\CaseAstring{case-A} |
| 179 |
\switch_new:Nn \TextCase {I~ don't~ know:~ #1\par} |
| 180 |
\switch_addcase:Nnn \TextCase {\CaseAstring} {A~ was~ used\par} |
| 181 |
\switch_addcase:Nnn \TextCase {case-B} {B~ was~ used\par} |
| 182 |
\ExplSyntaxOff |
| 183 |
\end{codestore} |
| 184 |
|
| 185 |
\begin{codestore}[switch01] |
| 186 |
\def\somemacro{case-A} |
| 187 |
\def\someothermacro{case-X} |
| 188 |
If B, then \TextCase{case-B} |
| 189 |
If A, then \TextCase{case-A} |
| 190 |
If X, then \TextCase{case-X} |
| 191 |
if somemacro: \TextCase{\somemacro} |
| 192 |
if someothermacro: \TextCase{\someothermacro} |
| 193 |
|
| 194 |
\end{codestore} |
| 195 |
|
| 196 |
\tscode*[switch]{switch01}[1] |
| 197 |
\tsexec{switch01}[1] |
| 198 |
|
| 199 |
To use the \tsobj[marg]{switch}, one just has to call it with \tsobj[marg]{case} as an argument. Note the possibility of using an auxiliary macro/command (which has to be fully expandable) as a \tsobj[marg]{case}. |
| 200 |
|
| 201 |
|
| 202 |
\tsdemo[switch]{switch01}[2] |
| 203 |
|
| 204 |
\section{Advanced Use} |
| 205 |
Since the resulting \tsobj[marg]{switch} is fully expandable (if the provided \tsobj[marg]{case-code}s also are), one can design the \tsobj[marg]{case-code}s to absorb more than one parameter/tokens. |
| 206 |
|
| 207 |
\begin{tsremark}[{\color{red}Careful:}] |
| 208 |
make sure that all \tsobj[marg]{case-code}s absorb the same number of parameters, to avoid ``leftovers'' or tricky errors. |
| 209 |
\end{tsremark} |
| 210 |
|
| 211 |
For instance, note the use of \tsobj{\@gobble} to absorb an unused parameter, or how \tsobj{\cmdY} is defined (with two parameters) then used with a ``fixed one''. The resulting command, \tsobj{\TCase}, absorbs 2 tokens/parameters: |
| 212 |
|
| 213 |
\begin{codestore}[switch03] |
| 214 |
\NewDocumentCommand \cmdX{m} {I got #1} |
| 215 |
\NewDocumentCommand \cmdY{mm} {Two: #1 and #2} |
| 216 |
\NewDocumentCommand \Astring{} {case-A} |
| 217 |
|
| 218 |
\makeatletter |
| 219 |
\newswitch \TCase {I~ don't~ know:~ #1 \@gobble} |
| 220 |
\makeatother |
| 221 |
\addcase \TCase {\Astring} {\cmdY{A~ given}} |
| 222 |
\addcase \TCase {case-B} {B~ was~ used. \cmdX} |
| 223 |
\end{codestore} |
| 224 |
|
| 225 |
\begin{codestore}[switch03] |
| 226 |
If B, then \TCase{case-B}{extra-B}\par |
| 227 |
If A, then \TCase{case-A}{extra-A}\par |
| 228 |
If X, then \TCase{case-X}{extra-X}\par |
| 229 |
\end{codestore} |
| 230 |
|
| 231 |
\tscode*[switch,emph={TCase},emph2={cmdX,cmdY,@gobble}]{switch03}[1] |
| 232 |
\tsexec{switch03}[1] |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
\tsdemo[switch,emph={TCase},emph2={cmdX,cmdY,@gobble}]{switch03}[2] |
| 237 |
|
| 238 |
Needless to say, the same applies under \tsobj[pkg]{expl3}. |
| 239 |
|
| 240 |
|
| 241 |
\begin{codestore}[switch04] |
| 242 |
\ExplSyntaxOn |
| 243 |
\cs_new:Npn \__cmdX:n #1 {I~ got~ #1} |
| 244 |
\cs_new:Npn \__cmdY:nn #1#2 {Two:~ #1~ and~ #2} |
| 245 |
\tl_new:N \l__case_tl |
| 246 |
\tl_set:Nn \l__case_tl {case-A} |
| 247 |
|
| 248 |
\switch_new:Nn \TxCase {I~ don't~ know:~ #1 \use_none:n} |
| 249 |
\switch_addcase:Nnn \TxCase {\l__case_tl} {\__cmdY:nn{A~ given}} |
| 250 |
\switch_addcase:Nnn \TxCase {case-B} {B~ was~ used.~ \__cmdX:n} |
| 251 |
\ExplSyntaxOff |
| 252 |
\end{codestore} |
| 253 |
|
| 254 |
\begin{codestore}[switch04] |
| 255 |
If B, then \TxCase{case-B}{extra-B}\par |
| 256 |
If A, then \TxCase{case-A}{extra-A}\par |
| 257 |
If X, then \TxCase{case-X}{extra-X}\par |
| 258 |
\end{codestore} |
| 259 |
|
| 260 |
\tscode*[switch,emph={TxCase},emph2={cmdX,cmdY,use_none:n}]{switch04}[1] |
| 261 |
\tsexec{switch04}[1] |
| 262 |
|
| 263 |
\tsdemo[switch,emph={TxCase},emph2={cmdX,cmdY,@gobble}]{switch04}[2] |
| 264 |
|
| 265 |
\printbibliography |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
\end{document} |