15

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.)

5
  • \newstring{thing} defines thing as the string, to be used in comparisons such as \ifprefix, \ifsuffix or \ifcontains. Definitely not to be used with \ifthenelse and you can't assign it a value.
    – egreg
    Sep 15, 2018 at 22:34
  • 2
    It would be nice to know (with some pseudocode) what you're after.
    – egreg
    Sep 15, 2018 at 22:43
  • can you not simply use \newcommand\thing{whatever} to be tested via \ifthenelse{\thing}{whatever}.... ? Sep 15, 2018 at 22:45
  • You have been become very familiar with TikZ, so why don't you just use pgfkeys for that? They allow you to do all sort of things, of course also to implement switches. One way is to say blabla/.code={\blablatrue}, of course you need to introduce \newif\ifblabla before. Then you can say \ifblabla\typeout{true}\else\typeout{false}\fi, where you can replace the typepouts with more meaningful commands.
    – user121799
    Sep 16, 2018 at 4:24
  • the answer has been posed but your original didn't work as you used \string whatever where \string makes (just) the w a catcode 12 punctuation character so not equal to the letter w, and then you have a spurious {...} around the \newcommand\result so that whichever branch is taken \result is defined but not visible after the test. Sep 16, 2018 at 7:21

2 Answers 2

15

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

5
  • My stupid error! Is the question worth deleting, given that?
    – murray
    Sep 16, 2018 at 13:45
  • @murray Why delete? You got other possibilities and explanations.
    – egreg
    Sep 16, 2018 at 13:54
  • I guess I've become accustomed to the severe standards of mathematica.stackexchange.com, where a question resulting from a simple syntactical error gets downvoted, quickly closed, and sometimes deleted.
    – murray
    Sep 17, 2018 at 13:02
  • @murray That's a very different planet. Here you even got four upvotes!
    – egreg
    Sep 17, 2018 at 13:23
  • Yes, I have! (I've been much more active on mathematica.stackexchange.com, simply because I know so much more about Mathematica than about TeX/LaTeX.)
    – murray
    Sep 18, 2018 at 15:07
2

Here is a way:

\documentclass{article}

 \def\thing{whatever}

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

\begin{document}

\result

\end{document}

Output:

thing is `whatever'.

1
  • I would suggest to use numbers instead and check with \ifnum\else\fi
    – koleygr
    Sep 16, 2018 at 1:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .