1

I'm trying to create a new environment for longtable enclosed by a filecontents environment. But I'm stuck and don't know exactly how to solve my problem.

\documentclass[a4paper,parskip]{scrartcl}
\usepackage{polyglossia}

\usepackage{longtable, ltxtable, filecontents, colortbl}

\newenvironment{tbl}{%
\begin{filecontents}{\jobname-ltxtable.tex}%
\begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}%
\hline\rowcolor[gray]{.8}%
Column 1 & Column 2 & Column 3 & Column 4\\\hline%
\endhead%
}{%
%\caption{#1} %
\end{longtable}%
\end{filecontents}%
\LTXtable{\textwidth}{\jobname-ltxtable.tex}%
}

\begin{document}

\begin{tbl}
1 & 2 & 3 & 4 \\\hline
1 & 2 & 3 & 4 \\\hline
1 & 2 & 3 & 4 \\\hline
\end{tbl}

\end{document}

I found out that using filecontents inside a macro leads to problems but I need something like filecontents to write out the longtable. The table shall later consist X columns. I reduced all the complexity to generate a minimal (non-working) example.

I hope someone can help me with this.

Thx + Bye!

  • unrelated to longtable, filecontents is a "verbatim" environment and like all verbatim codes can not be nested inside another command – David Carlisle Jun 3 '16 at 8:19
  • For a discussion regarding filecontents and newenvironment, see there: tex.stackexchange.com/q/130294/9075 – koppor Mar 7 '18 at 14:25
2

You can use the environ package to collect the body and then write it. Be aware that the body is written in one long line.

\documentclass[]{article}
\usepackage{polyglossia}

\usepackage{ltxtable,longtable,colortbl,environ,etoolbox}
\newwrite\mytabularwrite
\NewEnviron{tbl}
 {%
  \immediate\openout\mytabularwrite=\jobname-ltxtable.tex
   \immediate\write\mytabularwrite{%
    \unexpanded{%
     \begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}%
      \hline\rowcolor[gray]{.8}%
      Column 1 & Column 2 & Column 3 & Column 4\\\hline%
      \endhead}%
   }
  \immediate\write\mytabularwrite{\unexpanded\expandafter{\BODY}}%
  \immediate\write\mytabularwrite{\unexpanded{\end{longtable}}}%
  \immediate\closeout\mytabularwrite
 }

\AfterEndEnvironment{tbl}{\LTXtable{\textwidth}{\jobname-ltxtable2.tex}}
\begin{document}

\begin{tbl}
1äöü߀ & \textbf{2} & 3 & 4 \\\hline
1 & 2 & 3 & 4 \\\hline
1 & 2 & 3 & 4 \\\hline
\end{tbl}

\end{document}

enter image description here

  • sry for the late answer! Your code runs very well. Thanks a lot, Ulrike! – aronadaal Jun 29 '16 at 12:28

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.