3

Following this thread new command with cases / conditionals / 'if... then's and the answer of Peter Grill I tried to change the example to another usecase: to make gendering more easy in my document. There is a gender package available, but it does not provide what I need.

I setup the code in the following way, but I get always errors or the document is completely broken if it compiles (tried multiple things, like changing the "1" to an "m" - but obviously I have no clue what I am doing):

\documentclass{article}
\usepackage{xstring}

\newcommand{\genderhelp}[1]{% \IfEqCase{#1}{% {experten}{Experteninnen}% {forscher}{Forscherinnen}% % you can add more cases here as desired }[\PackageError{tree}{Undefined option to genderhelp: #1}{}]% }%

\begin{document} \genderhelp{experten}

\section{Die Hilfe von \genderhelp{forscher}}

\end{document}

So what I would like to achieve: give the command one word I already defined, and it outputs the defined string... Of course I could go search and replace all over the document, but that seems not the right way to go - also e.g. if the writing with the "*" is not correct anymore, I would only have one place to change that. And of course some more learning, how this Latex works ;)

Thanks a lot!

3 Answers3

5

If you simply want to replace a word by another there is no need for complicated xstring searches:

\documentclass{article}

\newcommand\GENDERexperten{Experteninnen} \newcommand\GENDERforscher{Forscherinnen} \newcommand{\genderhelp}[1]{% \ifcsname GENDER#1\endcsname \csname GENDER#1\endcsname \else \PackageError{tree}{Undefined option to genderhelp: #1}{}% \fi}

\begin{document} \genderhelp{experten}

\section{Die Hilfe von \genderhelp{forscher}}

\end{document}

Ulrike Fischer
  • 327,261
5
\documentclass{article}
\usepackage{xstring}

\NewDocumentCommand{\genderhelp}{m}{%
\IfEqCase{#1}{%
        {experten}{Experten*innen}%
        {forscher}{Forscher*innen}%
        % you can add more cases here as desired
}[\PackageError{tree}{Undefined option to genderhelp: #1}{}]%
}%

\begin{document}
\genderhelp{experten}

\section{Die Hilfe von \genderhelp{forscher}}

\end{document}
4

The obvious problem is that \genderhelp, as you defined it, is fragile. You can use \DeclareRobustCommand instead of \newcommand.

A better way is with expl3.

\documentclass{article}

%\usepackage{xparse} % uncomment for LaTeX prior to 2020-10-01

\ExplSyntaxOn \NewDocumentCommand{\setgenderhelpers}{m} { \tl_gput_right:Nn \g_genderhelp_list_tl { #1 } } \tl_new:N \g_genderhelp_list_tl

\NewDocumentCommand{\genderhelp}{m} { \str_case:nVF { #1 } \g_genderhelp_list_tl { \PackageError{tree}{Undefined option to genderhelp: #1}{} } } \ExplSyntaxOff

\setgenderhelpers{ {experten}{Experteninnen} {forscher}{Forscherinnen} }

\begin{document}

\genderhelp{experten}

\section{Die Hilfe von \genderhelp{forscher}}

\end{document}

You see that the setting of the helpers is easier. With this approach you can define a list of helpers in the .sty file, but users can add to the list as needs arise using \setgenderhelpers in their document preamble.

enter image description here

egreg
  • 1,121,712