5

I would like to save a command with its two arguments in an external file. I use LaTeX3 (I am learning LaTeX3). Here is a minimal file:

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\saveDataToFile}{}
    {
        \iow_new:N \g_data_stream 
        \iow_open:Nn \g_data_stream  { data-file.tex }
        \exp_args:NNx \iow_now:Nn \g_data_stream 
            { 
                \exp_not:N \myCommand
                {\exp_not:V \AA}
                {\exp_not:V \BB}
            }
        \iow_close:N \g_data_stream
    }               
\ExplSyntaxOff

\begin{document}

\def\AA{My first argument}
\def\BB{My \emph{second} argument}

\saveDataToFile

\end{document}

In the eternal file data-file.tex I get

\myCommand {My first argument}{My \emph {second} argument}

which is close to what I want. But I would like to get rid of the extra space between \myCommand and the first argument, as well as the one between \emph and its argument. At least I would like to understand why such spaces appear (fortunately they do not prevent my code to work). Thanks

5
  • 4
    That's the normal behaviour of TeX, it always puts a space after a control word when serialising tokens, nothing much you can do about it, I'm afraid. The space after \myCommand is easy as you have full control there (use \token_to_str:N instead of \exp_not:N), but whatever is in \AA and \BB is already tokenised. There might be solutions, but they are not fast, and it's simply not worth it, as TeX ignores these spaces anyway during tokenisation. Commented Oct 22, 2024 at 11:32
  • 3
    Aside: Instead of \exp_args:NNx \iow_now:Nn you should use \iow_now:Ne. Commented Oct 22, 2024 at 11:33
  • How are spaces problematic? Commented Oct 22, 2024 at 11:43
  • Thanks @Skillmon for your answers and advice. My code Is better now. Commented Oct 22, 2024 at 13:28
  • 1
    @projetmbc: this is not "problematic", this is surprising. But I understand now (answer below by @wipet) that this is how TeX deals with such a situation. Commented Oct 22, 2024 at 13:28

1 Answer 1

6

When you write \def\BB{My \emph{second} argument} then TeX saves to the macro body of \BB following tokens:

M y emph { s e c o n d } a r g u m e n t

If you write \def\BB{My \emph {second} argument} then TeX saves exactly the same tokens sequence to the macro body of \BB. It means that the information about existence or non-existence of the space after \emph is lost (during the tokenization process).

What TeX should do if you want to write such a token sequence to a file? It ads backslash before and one space after each multiletter control sequence like emph is. So the result is \emph .

Why the author of TeX decided about such feature? If one write \macro X then the tokenization process gives macro X and if we want to write this back to a file then we welcome the result \macro X and we don't want the result \macroX which cannot be tokenized again with the same result when the file is read again.

1
  • Thanks @wipet, I understand now why I have this behaviour. Commented Oct 22, 2024 at 13:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.