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}
Werner
  • 603,163
  • 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 Jan 28 '20 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. – Ulrike Fischer Jan 28 '20 at 11:47
  • @daleif surely you read the eloquent documentation of the ltxtable package? – David Carlisle Jan 28 '20 at 12:10
  • @DavidCarlisle ahh, with docs from 1995? – daleif Jan 28 '20 at 12:21
  • @UlrikeFischer I am not familiar with hacking definitions. How would I do that to add this default? – Gilean0709 Jan 28 '20 at 15:47
  • Sorry I won't tell you. I was one of the people who decided that there should be an explicit overwrite and I won't undermine this by telling in public how to change it. I can only advise you that if you don't know enough tex to do it yourself and understand the consequences then better don't do it at all. – Ulrike Fischer Jan 28 '20 at 16:11

1 Answers1

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.

Werner
  • 603,163
  • 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 Jan 28 '20 at 16:39