1

My problem, is to generate some .tex-file from within another .tex-file. The origin of problem is to extract some information from scientific paper, and create another .tex->.pdf-file with data about authors and so on.

My MWE now work with using \string, but if I want to create many lines in outputted .tex, it is easy to make a mistake. Is any simple solution?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
    %\usepackage{filecontents}
\makeatletter
\def\macroPass#1{%
    \if\relax\detokenize{#1}\relax
    \else
    \def\macroPrint{#1}
    \fi
}
\makeatother
\begin{document}


\newwrite\regformfile
\newcommand\writeregformfile{%
    \immediate\openout\regformfile=data.tex
    \immediate\write\regformfile{
            \string\documentclass{article}^^J
            \string\begin{document}^^J
                \string\begin{center} \string\bfseries^^J
                     \string\begin{tabular}{|l|l|}^^J
                         \string\hline^^J
                        Field& \unexpanded\expandafter{\macroPrint}  \string\\  \string\hline^^J
                     \string\end{tabular}^^J
                 \string\end{center}^^J
             \string\end{document}
    }
}

\macroPass{To data.tex}
\writeregformfile
\end{document}

MWE with folecontents, which is work incorrect.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
    %\usepackage{filecontents}
\makeatletter
\def\macroPass#1{%
    \if\relax\detokenize{#1}\relax
    \else
    \def\macroPrint{#1}
    \fi
}
\makeatother

\begin{filecontents*}{data.tex}
    \documentclass{article}
    \begin{document}
        \begin{center}\bfseries
            \begin{tabular}{|l|l|}
                \hline
                Field& \macroPrint \\ \hline
            \end{tabular}
        \end{center}
    \end{document}
\end{filecontents*}
\begin{document}
\macroPass{To data.tex}
\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39

1 Answers1

2

You can cheat and use a different escape character for defining the macro.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}

\newcommand\macroPass[1]{%
  \if\relax\detokenize{#1}\relax
  \else
    \def\macroPrint{#1}%
  \fi
}

\newwrite\regformfile
\begingroup
\catcode`?=0 \catcode`\\=12
?endlinechar=`^^J
?gdef?writeregformfile{%
  ?immediate?openout?regformfile=data.tex%
  ?immediate?write?regformfile{%
    \documentclass{article}
    \begin{document}
    \begin{center} \bfseries
    \begin{tabular}{|l|l|}
    \hline
    Field & ?unexpanded?expandafter{?macroPrint} \\ \hline
    \end{tabular}
    \end{center}
    \end{document}
  }%
}%
?endgroup%

\begin{document}

\macroPass{To data.tex}
\writeregformfile
\end{document}

The output file is as follows.

\documentclass{article}
\begin{document}
\begin{center} \bfseries
\begin{tabular}{|l|l|}
\hline
Field & To data.tex \\ \hline
\end{tabular}
\end{center}
\end{document}
egreg
  • 1,121,712