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.
\usepackage{etoolbox}
, but that's not the cause of your issue