# Write/Read values of the LaTeX commands (like \title, \ author etc) in file using Lua

This problem relate with other my question here . I found a solution with pdfpages package thanks to TX.SX:

\documentclass[]{scrbook}
\usepackage{authblk}
\usepackage{pdfpages}
\newcommand{\insertpaper}[4]%
{ % Syntax: \insertmydocument{Toc level}{Title}{Subtitle}{File}
% Requires: tocloft hyperref pdfpages
\newpage
\phantomsection
\cftchapterprecistoc{#3}
\includepdf[pages=-, pagecommand={\pagestyle{plain}}]{#4}
}
\begin{document}
\tableofcontents
\insertpaper{chapter}{Title o the 1st paper}{A.U. Thor11, A.U. Thor12}{paper1.pdf}
\insertpaper{chapter}{Title o the 2nd paper}{A.U. Thor21, A.U. Thor22}{paper2.pdf}
\end{document}


For production conference proceedings, I use pdfpages, as shown in example with LuaLaTeX. The macro \insertpaper adds a titles and authors of the papers in TOC. But for doing this, I manually write a desirable fields (e.g. title and authors) of the PDF. My papers (PDFs) also created by LuaLaTeX with scrartcl class and with authblk package for creating author and affiliations. Now my idea is not to manually fill corresponding fields, but read them from a file. That is, before paper.tex should write desirable fields into appropriate file.

It would be nice if the \insertpaper would have only one parameter filename and information about the required fields read from the appropriate information file.

Thus, I need some help for creation such procedures with Lua (desirable) or LaTeX3 or something else.

I found a some solution how to read the data

\documentclass{scrartcl}
\usepackage{luatextra}
\usepackage{filecontents}

\begin{filecontents}{testdata.dat}
Title of the page
A.U. Thor1
A.U. Thor2
\end{filecontents}

\begin{luacode}
file = io.open("testdata.dat", "r")
for line in file:lines() do

tex.print(line.."\\\\")
end
end
\end{luacode}

\begin{document}
\end{document}


but I have no idea how to substitude the data into desirable command, \insertpaper for example.

• You shouldn't use tocloft with KOMA. – Johannes_B Jan 16 '15 at 16:50

If you follow the suggestion given in my answer to How to write authors of an article to the file?, when you compile paper1.tex you'll have a file called paper1-titleauthors.dat which will contain something like

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


Now you can use the data:

\documentclass[]{scrbook}
\usepackage{authblk,tocloft}
\usepackage{pdfpages}
\usepackage{hyperref}
\newcommand{\insertpaper}[1]%
{% #1 is the file name (without extension)
\clearpage
\renewcommand\papertitle{}% reinitialize
\renewcommand\paperauthors{}% reinitialize
\input{#1-titleauthors.dat}%
\phantomsection
\includepdf[pages=-, pagecommand={\pagestyle{plain}}]{#1}%
}
\newcommand\papertitle{}% initialize
\newcommand\paperauthors{}% initialize
\makeatletter
\newcommand{\thispaperauthor}[2]{%
\ifx#2\finishauthors
% no more authors
\else