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!

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

1 Answer 1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .