[Tex/LaTex] How test value of a command with `\ifthenelse` and `\equal`

conditionalsifthenelsesyntaxxifthen

Revised question

(Trying to follow suggestion in comment by David Carlisle.)

I want to assign a string as the value of a command and then take alternative actions (by means of ifthenelse) according to whether the value of that string equals a particular value.

Example:

\documentclass{article}

 \newcommand{\thing}{whatever}

\usepackage{ifthen}
\ifthenelse{\equal{\thing}{\string whatever}}{%
  {\newcommand{\result}{thing is `whatever'.}}
  {\newcommand{\result}{thing is something else.}}%
}

\begin{document}

\result

\end{document}

What is wrong there with my \ifthenelse{\equal{\thing... test there? The command \result is never given a value, for a latex run gives error:

./whatever.tex:13: Undefined control sequence.
l.13     \result

Underlying purpose

In the actual project, which is book-length and involves a preamble consisting of some dozen "helper" files that are input in the root document, several "style" commands such as \thing are defined in root document in order to allow choosing among alternatives for each of various aspects of the overall document design, e.g., text width, font families, header styles. In those "helper" files, \ifthenelse tests are used for each such "style" command (instead of the simple-minded definition of \result shown in my MWE).

Thus I will be able to change such style parameters just by changing a word in the root document preamble, without having to burrow into the individual "helper" files and make edits there.

Original question

Using the generic TeX package texapi— or otherwise — (along with ifthen) I want to "declare" a string name, assign a string value to that name, and then take some action conditional upon that value.

For example:

\documentclass{article}

\input{texapi.tex}
\newstring{thing}
% HOW assign a value here to the string `thing'?

\usepackage{ifthen}
\ifthenelse{\equal{thing}{\string whatever}}{%
  {\newcommand{\result}{``It's whatever.''}}
  {\newcommand{\result}{``It's something else.''}}%
}

\begin{document}

\result

\end{document}

Is this possible and, if so, how?

(The packages I've looked at, namely, xstring and stringstring, give lots of commands for manipulating and testing strings, but do not seem to have commands to assign a string value.)

Best Answer

The code doesn't work because you're using wrong syntax for \ifthenelse.

Please check the syntax:

\documentclass{article}

\newcommand{\thing}{whatever}

\usepackage{ifthen}
\ifthenelse{\equal{\thing}{\string whatever}}
  {% True case
   \newcommand{\result}{thing is `whatever'.}%
  }
  {% false case
   \newcommand{\result}{thing is something else.}%
  }

\begin{document}

\result

\end{document}

This will print

this is something else.

because \string will make the w in the second argument different from the w in the (expansion of the) first argument to \equal.

Removing \string will give the expected result

This is ‘whatever’.


You seem to be using

\ifthenelse{<test>}{{<code for true>}{<code for false>}}

but this is not the right way:

\ifthenelse{<test>}{<code for true>}{<code for false>}

is the correct syntax.

In your code the <code for false> was \par (because you missed the third argument to \ifthenelse, so TeX picks up the \par generated by the blank line). Since the test returns false, nothing is defined.

On the other hand, if you remove \string so the test returns true, you'd end up with \result being undefined, because you'd execute two \newcommand instructions, but both inside a group.


For a more general approach:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\ifstringsequalTF}{mmmm}
 {
  \str_if_eq:eeTF { #1 } { #2 } { #3 } { #4 }
 }
\NewExpandableDocumentCommand{\stringcase}{mO{}m}
 {
  \str_case_e:nnF { #1 } { #3 } { #2 }
 }
\ExplSyntaxOff

\newcommand{\thing}{whatever}
\newcommand{\xyz}{xyz}
\newcommand{\noneoftheabove}{}

\ifstringsequalTF{\thing}{whatever}
  {\newcommand{\result}{thing is `whatever'.}}
  {\newcommand{\result}{thing is something else.}}

\begin{document}

\result

\stringcase{\thing}[OOPS]{
  {whatever}{found whatever}
  {xyz}{found xyz}
}

\stringcase{\xyz}[OOPS]{
  {whatever}{found whatever}
  {xyz}{found xyz}
}

\stringcase{\noneoftheabove}[OOPS]{
  {whatever}{found whatever}
  {xyz}{found xyz}
}

\end{document}

The optional argument to \stringcase can be omitted and in case of no match nothing would happen.

enter image description here

Related Question