0

I want to write value of the macro \@authortofile to a file. Inside the macro it is another macro which is defined as separator between some words:

\def\@separator{\def\@separator{, }}

When I try to do this I get in .dat file a string:

\thispaperauthor{\@separator One\@separator Two\@separator Three}

with non expanded \@separator, but I need text like:

\thispaperauthor{One, Two, Three}

PDF output iat the same time has the required form: enter image description here

Here is MWE

\documentclass[]{article}

\makeatletter
\def\@separator{\def\@separator{, }} %---def separator

\def\@authortofile{\@separator One\@separator Two\@separator Three} %def \@authortofile

%---definition of newwrite as \writedatatofile
\newwrite\titleauthorfile
\newcommand\writedatatofile[1]{%
  \immediate\openout\titleauthorfile=#1.dat
  \immediate\write\titleauthorfile{%
     \string\thispapertitle{\unexpanded\expandafter{\@title}}}%
  \immediate\write\titleauthorfile{%
     \string\thispaperauthor{\unexpanded\expandafter{\@authortofile}}}%
  \immediate\write\titleauthorfile{\string\finishauthors}
  \immediate\closeout\titleauthorfile
}
\makeatother
\title{Title}
\begin{document}
\writedatatofile{\jobname}

\makeatletter
\@authortofile
\makeatother

\end{document}
  • You ask about "totally expanded" but def\@separator{\def\@separator{, }} %---def separator does not work by expansion, so the question isn't that easy to answer. It would be possible to define a command that expanded to nothing the first time, or a comma after that but the definition there requires an assignment so is not expandable – David Carlisle Jan 22 '15 at 23:44
  • do you really want a space before the comma in the pdf? – David Carlisle Jan 22 '15 at 23:49
  • @DavidCarlisle No, I not need spase before comma, but after. – sergiokapone Jan 22 '15 at 23:50
  • looks very weird:-) – David Carlisle Jan 22 '15 at 23:50
  • @DavidCarlisle Main problem for me is to substitule \@separator by comma in .dat. Space before comma is just a typo – sergiokapone Jan 22 '15 at 23:53
5
\thispapertitle{Title}
\thispaperauthor{One , Two , Three}
\finishauthors

From

\documentclass[]{article}

\makeatletter
\def\@separator{\def\@separator{, }} %---def separator

\def\@authortofile{\@separator One \@separator Two \@separator Three} %def \@authortofile

%---definition of newwrite as \writedatatofile
\newwrite\titleauthorfile
\newcommand\writedatatofile[1]{%
  \immediate\openout\titleauthorfile=#1.dat
  \immediate\write\titleauthorfile{%
     \string\thispapertitle{\unexpanded\expandafter{\@title}}}%
{%
\def\@separator##1\@separator##2{%
\unexpanded{##1}%
\ifx!##2%
\else
, \expandafter\@separator\expandafter##2%
\fi
}%
  \immediate\write\titleauthorfile{%
     \string\thispaperauthor{\@authortofile\@separator!}}%
}%
  \immediate\write\titleauthorfile{\string\finishauthors}%%%
  \immediate\closeout\titleauthorfile
}
\makeatother
\title{Title}
\begin{document}
\writedatatofile{\jobname}

\makeatletter
\@authortofile
\makeatother

\end{document}
  • thanks. It's work. Strange construction \def\@separator##1\@separator##2{%. I would have guessed about. Where can I read something about programming in LaTeX? Can you recommend some literature? – sergiokapone Jan 23 '15 at 7:05
  • @sergiokapone texbook, or free alternative texbytopic – David Carlisle Jan 23 '15 at 8:40
  • Newer heard about texbytopic. Thanx! – sergiokapone Jan 23 '15 at 9:32
  • Just a comment for those (like me) who came here by searching for "write an expanded macro to a file": the key is "\unexpanded\expandafter". I have no idea why, but that's what worked for me. – Martin Argerami Nov 22 '15 at 17:29
  • 1
    @MartinArgerami \expandafter expands \@title once so revealing its definition then the \unexpanded prevents any macros in that definition being further expanded while being written out. – David Carlisle Nov 22 '15 at 17:34

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.