1

I created some new commands that take arguments. Several of my commands take the same (lengthy) arguments. Because of this I put those arguments in a separate file. But I seem unable to input or import these arguments.

main.tex:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myfirsttask}{m m m g}{
    #1 #2 #3
    % do stuff
    \IfNoValueF{#4}{%
            #4
            % do stuff
    }
}

\NewDocumentCommand{\mysecondtask}{m m m g}{
    #1 #2 #3
    % do stuff
    \IfNoValueF{#4}{%
            #4
            % do stuff
    }
}

\NewDocumentCommand{\myfirst}{m}{
    \expandafter\myfirsttask\input{#1}
}

\NewDocumentCommand{\mysecond}{m}{
    \expandafter\mysecondtask\input{#1}
}
\ExplSyntaxOff

\begin{document}

    \myfirst{argumentsFile}

    \mysecond{argumentsFile}

\end{document}

argumentsFile.tex:

{first argument}%
{second argument}%
{third argument}%
{optional argument}

At first I did not use \expandafter and got:

Runaway argument?
! Paragraph ended before \myfirsttask  was complete.
<to be read again> 
                   \par

I assumed that \myfirsttask and \mysecondtask dit not have enough arguments as the contents of the argumentsFile were not yet inserted. Therefore I added \expandafter as to insert the argumentsFile before expanding \myfirsttask and \mysecondtask. This however gives me

! LaTeX Error: File `-NoValue-.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name: 
! Emergency stop.
<read *> 

l.34    \myfirst{argumentsFile}

I assume that's because the curly braces in my argumentsFile were removed by the expansion of \input???

Does anyone know how I could correctly import/input command arguments?

1

\input is not expandable in one step (in LaTeX, the \input primitive is).

You can absorb the contents of the file and then deliver it:

\begin{filecontents*}{\jobname-ArgumentsFile}
{first argument}%
{second argument}%
{third argument}%
{optional argument}
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myfirsttask}{m m m g}
  {
    #1~#2~#3
    % do stuff
    \IfNoValueF{#4}{%
            (#4)
            % do stuff
    }
  }

\NewDocumentCommand{\myfirst}{m}
  {
    \tl_set_from_file:Nnn \l_tmpa_tl { } { #1 }
    \exp_last_unbraced:NV \myfirsttask \l_tmpa_tl
  }

\ExplSyntaxOff

\begin{document}

\myfirst{\jobname-ArgumentsFile}

\end{document}

enter image description here

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.