GeneralCustom macro with parameter (switch-case?)

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Kreso
Posts: 3
Joined: Tue Jun 17, 2008 1:43 am

Custom macro with parameter (switch-case?)

Post by Kreso »

I'm writing an article about Egyptian hieroglyphs and I am including individual hieroglyphs as inline graphics. Here is a minimal example:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{scalerel}

\newcommand{\quail}{\scalerel*{\includegraphics{G43.pdf}}{\strut} }

\setstretch{1}

\begin{document}
Lorem ipsum dolor sit amet consectetur \quail adipiscing elit euismod, nunc tellus senectus faucibus dapibus potenti congue dignissim.
\end{document}
The macro \quail here just includes the picture from the file "G43.pdf", which is a hieroglyph of quail chick. Now, I also have the macro \owl, and \vulture and \frog, and it's getting a bit difficult to keep track of all of them, and the macro namespace is getting polluted.

I'll like to (but I don't know how to) define a macro (let's name it \hg for hieroglyph) that accepts an argument, so that for each of the possible values of the argument, a different set of commands is substituted.

For example

Code: Select all

\hg{w} 
gets substituted with

Code: Select all

{\scalerel*{\includegraphics{G43.pdf}}{\strut} }
but something else, like

Code: Select all

\hg{3} 
with

Code: Select all

{\scalerel*{\includegraphics{G1.pdf}}{\strut} }
So, in essence, I'm looking for some sort of a switch-case statement, to specify different commands for each possible argument value, which can be a letter, or a number, or a string... And if an undefined argument is passed (like \hg{wrong}), an error is thrown during compilation.

How would one go about achieving that?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Custom macro with parameter (switch-case?)

Post by rais »

You could try something like

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\renewcommand*\includegraphics[2][]{%
% omit this redefinition for the real thing
  \typeout{I don't have your graphics...}%
  \fbox{#2}%
}
\usepackage{scalerel}
\newif\ifcaseclosed
\newcommand*\switchresult{}
\newenvironment{switch}[1]{%
  \edef\switcharg{#1}%
  \global\caseclosedfalse
  \providecommand*\case[2]{%
    \ifcaseclosed\else
      \edef\casesearch{##1}%
      \ifx\casesearch\switcharg
        \global\caseclosedtrue
        \edef\casearg{##2}%
      \fi
    \fi
  }%\case{search}{arg}
}{%at end of switch evironment:
  \ifcaseclosed
% output \casearg here
    \typeout{`\switcharg' found (result is `\casearg')}
    \xdef\switchresult{\casearg}%
  \else
%output error here
    \typeout{`\switcharg' not found.}
  \fi
}% switch env
\newcommand*\hg[1]{%
  \begin{switch}{#1}
    \case{3}{G1}
    \case{w}{G43}
    \case{quail}{G43}
% to be continued
  \end{switch}
  \ifcaseclosed
% do something with \switchresult:
    \scalerel*{\includegraphics{\switchresult}}{\strut}
  \else
    ???%
  \fi
}
\begin{document}
\hg{3}

\hg{w}

\hg{fail}
\end{document}
Note that I haven't turned a wrong input into an error as such, just a message, so I opted for making \ifcaseclosed available after the switch environment finished---hence the \global before the \caseclosed{true|false} statements.

KR
Rainer
Kreso
Posts: 3
Joined: Tue Jun 17, 2008 1:43 am

Custom macro with parameter (switch-case?)

Post by Kreso »

Yes! This is exactly what I had in mind. It's an excellent starting point for experimentation, I have enough to figure it out now.

Thank you so much!
Post Reply