3

I am writing a paper that includes simulation results from performing a number of trials of an experiment. Each of the output figures (.pdfs) I produce includes the number of trials in the filename (e.g. foo_10_trials.pdf, foo_100_trials.pdf, bar_10_trials.pdf, bar_100_trials.pdf) where the number itself is based off a variable set in a Bash script which is writing the files. Because the experiments take a while to run, it's convenient to use the low number of trials figures while I'm writing the paper and then include a greater number of trial figures in the final paper. The issue is I then need to go through and update the number of trials e.g. change all 10s --> 100s.

What I'm trying to do is have a file vars/trials.tex that simply includes an integer I can change e.g. 100. Then in my main manuscript I have \newcommand{\trials}{\input{vars/trials}}. I can use this in-text fine, but what I'd like to be able to do is define my figures in such a way that they use this variable too, e.g.

\newcommand{\fooFigure}[0]{
\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{foo_\trials{}_trials}
    \caption{Foo caption.}
    \label{fig:foo}
\end{figure}
}

\fooFigure

This way, I can update all the figures and all the in-text references by simply changing the trials.tex file to e.g. read 100 instead of 10. The problem I'm running into with \includegraphics is it doesn't seem to accept \trials{} as I have it above. The error I'm getting is:

ERROR: Undefined control sequence.

--- TeX said ---
\filename@simple ...#2\\}\fi \edef \filename@base 
                                                  {#1}
l.645 \fooFigure

--- HELP ---
TeX encountered an unknown command name. You probably misspelled the
name. If this message occurs when a LaTeX command is being processed,
the command is probably in the wrong place---for example, the error
can be produced by an \item command that's not inside a list-making
environment. The error can also be caused by a missing \documentclass
command.

I'm by no means an expert in TeX/LaTeX, so I don't know if it's an issue of the \trials or \input{vars/trials} not being expanded for example or something else.

  • 1
    It should be either \newcommand{\fooFigure}{... or \newcommand{\fooFigure}[1]{...\includegraphics[width=\textwidth]{foo_/trials{}_#1}.... – user11232 Nov 6 '14 at 4:34
  • That is, you never create a newcommand that takes 'zero' arguments by telling it to take '0' arguments: only when there is one or more arguments do you add something like [1] or [2] and so on. – jon Nov 6 '14 at 4:44
  • I think \trials is suppose to be a macro, in which case one also needs a \newcommand{\trials}{100} or \def\trials{100}. One can also use {\trials} to insert a macro into the middle of string. – John Kormylo Nov 6 '14 at 5:21
5

Getting the number of trials

From the TeX side it is easier, if the bash script could write a complete definition in var/trials.tex, e.g.

\renewcommand*{\trials}{100}

Then the file can be easily read by TeX:

\providecommand*{\trial}{10}% default setting
\input{var/trials}

If the file var/trials.tex only contains an integer, then it can be read via package catchfile:

\usepackage{catchfile}
\CatchFileEdef{\trials}{var/trials.tex}{\endlinechar=-1 }

The last command removes the line ends of var/trials.tex to avoid additional white space after the number.

Using \trials

Macro \trials as defined above does not have arguments. An additional pair of braces are therefore not removed and would become part of the file name. A space suffices to end macro \trials and the space will not be part of the file name (e.g. foo\trials bar would become foo100bar). But since a period is following, also the space is not necessary:

\includegraphics[...]{foo_\trials.pdf}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.