0
\documentclass{article}
\usepackage{ifthen}
\usepackage{currfile}

\newcommand{\textforpicture}{
\ifthenelse{John}{This is text 1 and the name of this file is \currfilebase}{nothing}
\ifthenelse{Jack}{This is text 2 and the name of this file is \currfilebase}{nothing}
\ifthenelse{Jill}{This is text 3 and the name of this file is \currfilebase}{nothing}}

\batchmode

\begin{document}

Experimental text fragement for John: \textforpicture{John}\\
Experimental text fragement for Jack: \textforpicture{Jack}\\
Experimental text fragement for Jill: \textforpicture{Jill}\\


\end{document}

Hello everyone, I am working on a report for some pupils and I have got the this idea: I would like to add text for each pupil based on a "database" comprising their names and some text blocks. If the name of the pupil is mentioned the respective text fragment (describing a picture) should be inserted and if possible the respective file name should also be mentioned. For this I tried the above code but ended up in a total mess. The compiled outcome is completely useless. Does anyone has got an idea how to solve the dilemma? Thank in advance!

2

1 Answer 1

4

I do not really understand, what you are trying, but the first argument of \ifthenelse should surely be a comparison, maybe something like:

\documentclass{article}
\usepackage{ifthen}
\usepackage{currfile}

\newcommand{\textforpicture}[1]{%
  \ifthenelse{\equal{#1}{John}}
    {This is text 1 and the name of this file is \currfilebase}%
    {\ifthenelse{\equal{#1}{Jack}}
      {This is text 2 and the name of this file is \currfilebase}%
      {\ifthenelse{\equal{#1}{Jill}}
        {This is text 3 and the name of this file is \currfilebase}
        {nothing}
      }%
    }%
}%
  

\begin{document}

Experimental text fragement for John: \textforpicture{John}

Experimental text fragement for Jack: \textforpicture{Jack}

Experimental text fragement for Jill: \textforpicture{Jill}

\end{document}

enter image description here

For such cases, you can also use l3:

\documentclass{article}

\ExplSyntaxOn
%\newcommand{\textforpicture}[1]% could be used, but using
\NewDocumentCommand \textforpicture { m } % is usual for defining a user command in l3 context.
  {
    \str_case:nnF { #1 }
      {
        {John} {This~is~text~1}
        {Jack} {This~is~text~2}
        {Jill} {This~is~text~3}
      }
      {nothing}
  }%
\ExplSyntaxOff  

\begin{document}

Experimental text fragement for John: \textforpicture{John}

Experimental text fragement for Jack: \textforpicture{Jack}

Experimental text fragement for Jill: \textforpicture{Jill}

\end{document}

I've removed currfile in this second example, because I've not understood, why you are using it. And IMHO it is also not needed to show, how to use \str_case:nnF.

Please have a look into “The LaTeX3 interfaces” for more information about using LaTeX3.

3
  • sorry if my question was kind of difficult to understand but you nailed it! Thank you very much for your useful answer!
    – mario1000
    Commented Jun 19 at 17:01
  • May I ask another question related to this (already solved) problem? I tried both of your solutions and in the second case all word are stick together if I don´t add a tilde in between the words. Does this behavior is intended and how can I get all the tilde in between the words if the sentence is long (find and replace or is there any kind of command in LaTeX?) Thank you!
    – mario1000
    Commented Jun 19 at 18:05
  • @mario1000 I've linked a manual, that explains this. And yes, in l3 context ignoring spaces (and empty lines) is intended. If you have more questions about latex3 please ask them as new questions, if you cannot find a similar question.
    – cabohah
    Commented Jun 19 at 19:41

You must log in to answer this question.

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