2

I have a macro \writetitletofile which is write title of the article to the .dat file. How can I do the same with authors of an article. It should be noted that list of authors create with authblk package.

\documentclass{scrartcl}
    \usepackage{fontspec, polyglossia}
    \setmainlanguage{english}
    \setmainfont{CMU Serif}                 
    \setsansfont{CMU Sans Serif}            
    \setmonofont{CMU Typewriter Text}
    \usepackage{authblk}
    \author[1]{First Author}
    \affil[1]{Affiliation of the 1st author}
    \author[2]{Second Author}
    \affil[2]{Affiliation of the 2nd author}    
    \title{Title of the article}    
    \makeatletter
    \def\writetitletofile{%
      \newwrite\file
      \immediate\openout\file=titleauthors.dat
      \immediate\write\file{\@title}%
      \immediate\closeout\file%
    }%
    \makeatother        
\begin{document}
\writetitletofile
\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39

1 Answers1

4

We can use the fact that authblk saves the list of authors and affiliations in a well defined format: in the case of the example, the macro \AB@authors expands to

\protect \Authfont \protect \AB@setsep First Author\protect \\[\affilsep ]%
\protect \Affilfont Affiliation of the 1st author\protect \and \protect
\Authfont \protect \AB@setsep Second Author\protect \\[\affilsep ]%
\protect \Affilfont Affiliation of the 2nd author.

(reformatted for clarity).

So we can use a recursive macro for extracting the data between \AB@setsep and \protect.

\documentclass{scrartcl}
\usepackage{fontspec, polyglossia}
\usepackage{authblk}

\setmainlanguage{english}
\setmainfont{CMU Serif}                 
\setsansfont{CMU Sans Serif}            
\setmonofont{CMU Typewriter Text}

\makeatletter
\newwrite\titleauthorfile
\newcommand\writedatatofile{%
  \immediate\openout\titleauthorfile=\jobname-titleauthors.dat
  \immediate\write\titleauthorfile{\@title}%
  \processauthors
  \immediate\closeout\titleauthorfile
}
\newcommand\processauthors{%
  \expandafter\process@authors\AB@authors\AB@setsep\protect\@nil
}
\def\process@authors#1\AB@setsep#2\protect#3\@nil{%
  \ifx\relax#2\relax
    \expandafter\@gobble
  \else
    \immediate\write\titleauthorfile{\unexpanded{#2}}%
    \expandafter\@firstofone
  \fi
  {\process@authors#3\@nil}%
}
\makeatother        


\author[1]{First Author}
\affil[1]{Affiliation of the 1st author}
\author[2]{Second Author}
\affil[2]{Affiliation of the 2nd author}    
\title{Title of the article}    
\begin{document}
\writedatatofile
\end{document}

The contents of the file \jobname-titleauthors.dat (\jobname is the name of the main file, it's better to use a name depending on it) will be

Title of the article
First Author
Second Author

For successive reading of the created file it's better to add some structure:

\documentclass{scrartcl}
\usepackage{fontspec, polyglossia}
\usepackage{authblk}

\setmainlanguage{english}
\setmainfont{CMU Serif}                 
\setsansfont{CMU Sans Serif}            
\setmonofont{CMU Typewriter Text}

\makeatletter
\newwrite\titleauthorfile
\newcommand\writedatatofile{%
  \immediate\openout\titleauthorfile=\jobname-titleauthors.dat
  \immediate\write\titleauthorfile{%
     \string\thispapertitle{\unexpanded\expandafter{\@title}}}%
  \processauthors
  \immediate\write\titleauthorfile{\string\finishauthors}
  \immediate\closeout\titleauthorfile
}
\newcommand\processauthors{%
  \expandafter\process@authors\AB@authors\AB@setsep\protect\@nil
}
\def\process@authors#1\AB@setsep#2\protect#3\@nil{%
  \ifx\relax#2\relax
    \expandafter\@gobble
  \else
    \immediate\write\titleauthorfile{%
      \string\thispaperauthor{\unexpanded{#2}}%
    }
    \expandafter\@firstofone
  \fi
  {\process@authors#3\@nil}%
}
\makeatother        


\author[1]{First Author}
\affil[1]{Affiliation of the 1st author}
\author[2]{Second Author}
\affil[2]{Affiliation of the 2nd author}    
\title{Title of the article}    
\begin{document}
\writedatatofile
\end{document}

Now the output file would be

\thispapertitle{Title of the article}
\thispaperauthor{First Author}
\thispaperauthor{Second Author}
\finishauthors

And you can \input this file after giving sensible definitions for \thispapertitle and \thispaperauthor.

egreg
  • 1,121,712
  • Thanks! How can I read this records? I need to create command \insertpaper{Title of the article}{First Author, Second Author}{file_name_which_I_read_this_fields.pdf} This question relate with this one http://tex.stackexchange.com/questions/223471/write-read-values-of-the-latex-commands-like-title-author-etc-in-file-usin – sergiokapone Jan 17 '15 at 01:45