I am trying to write into an auxiliary file from a command with one argument. The written text is another \createcommand with one argument too. When I use "#1" the argument of meta-command is expanded. It's ok. The problem is how to literally write "#1" referencing the argument of command created in the auxiliary file. I tried \#1, ^^23 1 and \noexpand#1, but it didn't work.

This is my simplified example. The problem is the last #1. In this case I used "\\#1", but this adds an undesirable slash.


        \noexpand\newcommand{\csname linktopage#1\endcsname}[1]{
            {\#1 }

Using \writeChapterCounter{ChapToc} it produces:

 \newcommand {\linktopageChapToc }[1]{ \hyperlink {topofpage.\arabic{chChapToc}} {\#1 } }


  • 1
    unrelated to # question, but you are missing a lot of % at ends of lines. – David Carlisle Jun 26 '15 at 16:24

You want \string# in the \write but to refer to # in the body of a definition you need ## so

  • There is no need to write #1 at all, because the argument of the new defined macro gets at the end of the definition text as last argument of \hyperlink. Then the new command can be defined without arguments and the argument in the input is later taken by \hyperlink directly.

  • There are lots of (unwanted) spaces by line ends.

  • I have added \expandafter\noexpand before the new command \csname ...\endcsname. It does not change the behavior, if the new command is undefined, because \csname automatically assigns it the meaning of \relax, which is not further expanded. But if the command is defined and can be expanded, the expansion would be written into the file, probably confusing \newcommand.


      \expandafter\noexpand\csname linktopage#1\endcsname

Then \writeChapterCounter{ChapToc} will produce:

\newcommand {\linktocpageChapToc }{\hyperlink {topofpage.\arabic {chChapToc}}}

And \linktocpageChapToc{foobar} will expand to

\hyperlink {topofpage.\arabic {chChapToc}}{foobar}

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.