8

I have a collection of latex documents. I want to output some latex commands to an auxiliary file corresponding to each document. All these aux files will be later used (along with their TOCs) to compile a separate document. The documents MWE are like -

filename = maths.tex

\documentclass{book}
\newcommand{\tocabbreviation}{Something specific to the document}
\newcommand{\tocfn}[1]{Some command #1}
\usepackage{etoolbox}
%\include{preamble} % contains common commands, not needed in MWE
\edef\leftbracechar{\string{}
\edef\rightbracechar{\string}}
\ifdef{\tocabbreviation} {
\newwrite\myfile
\immediate\openout\myfile=\jobname_aux.tex
\immediate\write\myfile{\string\newcommand \leftbracechar \string\ \jobname tocfn\rightbracechar \leftbracechar \string\tocfn \leftbracechar \string\tocabbreviation\rightbracechar\rightbracechar} 
\immediate\closeout\myfile 
} { }
\begin{document}
abra ca dabra
\end{document}

I would like to see this output in maths_aux.tex

\newcommand{\mathstocfn}{\tocfn{\tocabbreviation}}

so that when i later compile a new file and input{maths_aux.tex} in it I get a command mathstocfn defined.

instead I see this

\newcommand{\ mathstocfn}{\tocfn{\tocabbreviation}}

How do I get rid of this pesky space? I have tried all kinds of suggestions - textbackslash, backslashchar, macros and what not. it does everything except what i want.

1
  • Welcome to TeX.SE. First of all: Your document is missing \usepackage{etoolbox}, but that's not the cause of your issue
    – user31729
    Mar 12, 2017 at 7:03

1 Answer 1

9

You can use

\expandafter\string\csname\jobname tocfn\endcsname

that will apply \string to an already formed control sequence token. I also changed the code to write the current expansion of \tocabbreviation, or it would be lost.

\documentclass{book}
\usepackage{etoolbox}

\newcommand{\tocabbreviation}{Something specific to the document}
\newcommand{\tocfn}[1]{Some command #1}

\ifdef{\tocabbreviation}{%
  \newwrite\myfile
  \immediate\openout\myfile=\jobname_aux.tex
  \immediate\write\myfile{%
    \string\newcommand{%
      \expandafter\string\csname \jobname tocfn\endcsname
    }{\string\tocfn{\unexpanded\expandafter{\tocabbreviation}}}%
  }
  \immediate\closeout\myfile
}{}

\begin{document}

abra ca dabra

\end{document}

Contents of the magguu_aux.tex file:

\newcommand{\magguutocfn}{\tocfn{Something specific to the document}}

Note you don't need \leftbracechar and \rightbracechar unless you plan to write unbalanced strings.

An expl3 version:

\documentclass{book}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\settocabbreviation}{m}
 {
  \magguu_write:xn { \exp_not:c { \c_sys_jobname_str tocfn } } { #1 }
 }

\iow_new:N \g_magguu_toc_out_stream

\cs_new_protected:Nn \magguu_write:nn
 {
  \iow_open:Nn \g_magguu_toc_out_stream { \c_sys_jobname_str _aux.tex }
  \iow_now:Nn \g_magguu_toc_out_stream { \newcommand{#1}{#2} }
  \iow_close:N \g_magguu_toc_out_stream
 }
\cs_generate_variant:Nn \magguu_write:nn { x }
\ExplSyntaxOff

\settocabbreviation{Something specific to the document}

\begin{document}

abra ca dabra

\end{document}

You can add \newcommand{\tocabbreviation}{#1} to the code for \settocabbreviation if you need to use \tocabbreviation in the document.

1
  • yes. i actually used the current expansion of \tocabbreviation in my real document. each of my document has its own definition of \tocabbreviation.
    – magguu
    Mar 12, 2017 at 9:09

You must log in to answer this question.

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