1

I recently updated my LaTeX-Distribution and found out afterwards that the filecontents and ltxtable combination was broken. Only one table was loaded multiple times in my document. Afterwards I found out that the filecontents-package became obsolete (filecontents: This package is obsolete) with the 2019-10-01 version of the LaTeX kernel and is now a base functionality. However, the new default is that files are not overwritten, which can be changed with an additional option \begin{filecontents}[overwrite](filename).

I was wondering if there is a way to change it globally instead of every instance?

I included a short example to show the current problem:

\documentclass{article}
\usepackage{ltxtable}

\begin{document}

 \begin{filecontents}{tmptable}
    \begin{longtable}{l X}
        Table 1 & text 1
    \end{longtable}
\end{filecontents}
\LTXtable{\textwidth}{tmptable}

\begin{filecontents}{tmptable}
    \begin{longtable}{l X}
        Table 2 & text 2
    \end{longtable}
\end{filecontents}
\LTXtable{\textwidth}{tmptable}

\end{document}
6
  • Wondering here. What exactly are you saving by putting this table into an external file? instead of in an internal variable. Then there is no IO involved.
    – daleif
    Commented Jan 28, 2020 at 11:37
  • Well you can hack the definition and add such a default. But it was a quite concious decision that overwrite should be set explicitly. I didn't like it at all that with the filecontents package files were suddenly overwritten without warning. Commented Jan 28, 2020 at 11:47
  • @daleif surely you read the eloquent documentation of the ltxtable package? Commented Jan 28, 2020 at 12:10
  • @DavidCarlisle ahh, with docs from 1995?
    – daleif
    Commented Jan 28, 2020 at 12:21
  • @UlrikeFischer I am not familiar with hacking definitions. How would I do that to add this default?
    – Gilean0709
    Commented Jan 28, 2020 at 15:47

1 Answer 1

2

The following simple patch adds the switch supplied by overwrite (or force) by default:

\makeatletter
\let\oldfilec@ntents\filec@ntents
\gdef\filec@ntents{\filec@ntents@overwrite\oldfilec@ntents}
\makeatother

If the above doesn't make sense, one could always be a bit more verbose. To that end, you can store the old definitions and then redefine both the filecontents and filecontents* environments using environ:

enter image description here

\documentclass{article}

\usepackage{environ}

% Update filecontents environment to overwrite by default
\let\oldfilecontents\filecontents
\let\endoldfilecontents\endfilecontents
\RenewEnviron{filecontents}[2][overwrite]{%
  \begin{oldfilecontents}[#1]{#2}
    \BODY
  \end{oldfilecontents}
}
% Update filecontents* environment to overwrite by default
\expandafter\let\expandafter\filecontentsstar\csname filecontents*\endcsname
\expandafter\let\expandafter\endfilecontentsstar\csname endfilecontents*\endcsname
\RenewEnviron{filecontents*}[2][overwrite]{%
  \begin{filecontentsstar}[#1]{#2}
    \BODY
  \end{filecontentsstar}
}

\begin{document}

\begin{filecontents}{myfile.tex}
File 1
\end{filecontents}

\input{myfile}

\begin{filecontents*}{myfile.tex}
File 2
\end{filecontents*}

\input{myfile}

\end{document}

Both definitions above call the original environments with overwrite as a default option only when no other options are specified.

1
  • Thank you. The first solution works great. Interestingly, the second solution works, if I only use filecontents, but it doesn't work together with ltxtable and returns an error: Extra alignment tab has been changed to \cr. \end{filecontents}.
    – Gilean0709
    Commented Jan 28, 2020 at 16:39

You must log in to answer this question.

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