0

I need to pass some string of text from my LuaLaTeX document to be used as a file name. Here's a kind of MWE:

\documentclass{article}
\usepackage{luamplib}
\def\testfilename{test1}
\def\writesomewords#1#2{
    \begin{mplibcode}
        beginfig(0);
        string somewords;
        somewords = "somewords";
        draw textext("#1");
        draw textext("#2") shifted (0, -1cm);
        write somewords to "#1.txt";
        write EOF to "#1.txt";
        write somewords to "#2.txt";
        write EOF to "#2.txt";
        endfig;
    \end{mplibcode}
    }
\begin{document}
    \writesomewords{\testfilename}{test2}
\end{document}

Most of the time arguments are passed just fine, and both draw textext("..."); clauses produce the expected result:

enter image description here

However, when it comes to file names, whereas the second file is named test2.txt as expected, the first file ends up called \testfilename for some reason (as the original macro, which holds the intended name). Apparently, it doesn't expand in this particular case.

This might be a bug (which is somewhat likely, given that an analogous code works fine in ConTeXt), in which case I'll report it, but it just might be an intended behavior, in which case: how do I expand the file name so it does end up as a file name in the end?

2 Answers 2

1

mplibcode prevents expansion but you can use \expanded to expand the arguments first

\documentclass{article}
\usepackage{luamplib}
\def\testfilename{test1}
\def\writesomewords#1#2{
    \expanded{\noexpand\begin{mplibcode}
        beginfig(0);
        string somewords;
        somewords = "somewords";
        draw textext("#1");
        draw textext("#2") shifted (0, -1cm);
        write somewords to "#1.txt";
        write EOF to "#1.txt";
        write somewords to "#2.txt";
        write EOF to "#2.txt";
        endfig;
    \noexpand\end{mplibcode}}
    }
\begin{document}
    \writesomewords{\testfilename}{test2}
\end{document}

produces test1.txt

somewords

and

enter image description here

2
  • mplibcode allows for LaTeX commands in its body, for later processing, which is the reason for suppressing expansion, so doing full expansion of the body might not be the best strategy.
    – egreg
    Commented Dec 12, 2023 at 21:04
  • yes sure, but here that isn't an issue. you could always use \noexpand as shown for \begin if a different version required latex commands in the body (or \unexpanded...} @egreg Commented Dec 12, 2023 at 21:10
1

I'd go with an auxiliary macro to which you pass the expanded arguments:

\documentclass{article}
\usepackage{luamplib}

\newcommand{\testfilename}{test1}
\newcommand{\writesomewords}[2]{%
  \ExpandArgs{ee}\writesomewordsaux{#1}{#2}%
}
\newcommand{\writesomewordsaux}[2]{%
  \begin{mplibcode}
    beginfig(0);
    string somewords;
    somewords = "somewords";
    draw textext("#1");
    draw textext("#2") shifted (0, -1cm);
    write somewords to "#1.txt";
    write EOF to "#1.txt";
    write somewords to "#2.txt";
    write EOF to "#2.txt";
    endfig;
  \end{mplibcode}%
}

\begin{document}

\writesomewords{\testfilename}{test2}

\end{document}

If you're running an older LaTeX distribution, replace with

\newcommand{\writesomewords}[2]{%
  \expanded{\noexpand\writesomewordsaux{#1}{#2}}%
}

You must log in to answer this question.

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