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:
This will print
because
\string
will make thew
in the second argument different from thew
in the (expansion of the) first argument to\equal
.Removing
\string
will give the expected resultYou seem to be using
but this is not the right way:
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:
The optional argument to
\stringcase
can be omitted and in case of no match nothing would happen.