
Aquí hay dos preguntas sobre filecontents
el medio ambiente:
¿Es posible agregar contenido a un archivo preexistente o, como mínimo, mantener abierto el archivo externo para agregar el contenido de varios
filecontents
entornos al mismo archivo externo?¿Es posible saber cuántas líneas ha escrito un
filecontents
entorno en el archivo externo?
Respuesta1
Contar las líneas filecontents
es fácil porque el entorno procesa su contenido línea por línea, lee, verifica si el entorno terminó y luego escribe. Sólo necesitas hacer un contador y agregarlo cada vez que se escribe una línea.
Agregar es más complicado. En primer lugar, TeX (al menos sin Lua) no puede abrir archivos con permiso "añadir", por lo que eso está descartado. La idea de dejar el archivo abierto y luego reanudar la escritura más tarde parece más fácil al principio, pero necesitaría guardar un montón de variables en todos los entornos y luego no podrá escribir en el archivo a
, luego b
y luego agregar a a
.
En el código debajo de la \filecontents@preappend
macro (cuando append
se usa la opción) lee el archivo en el argumento de una macro temporal y luego \filecontents@append
coloca la macro de lectura en el flujo de entrada antes del contenido real del entorno, por lo que funciona como si Escribiste todo de una vez. La append
opción implica las opciones overwrite
y noheader
.
Estaba intentando escribir el código como un parche, para que sea más compacto, pero aparentemente al parchear un comando se pierden los ^^J
caracteres y luego el encabezado se colapsa en una sola línea, por lo que esta es una redefinición completa de \filec@ntents
, pero las líneas modificadas están marcadas.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \FileSize \file_size:n
\ExplSyntaxOff
\makeatletter
\begingroup%
\@tempcnta=1
\loop
\catcode\@tempcnta=12 %
\advance\@tempcnta\@ne %
\ifnum\@tempcnta<32 %
\repeat %
\catcode`\^^M\active
\catcode`\^^L\active\let^^L\relax
\catcode`\^^I\active
\gdef\filec@ntents#1{%
\set@curr@file{\filec@ntents@checkdir#1}%
\edef\q@curr@file{\expandafter\quote@name\expandafter{\@curr@file}}%
\filecontents@preappend% <- ADDED for appending
\gaborit@reset@counter% <- ADDED for counting lines
\gaborit@reset@append% <- ADDED for counting lines
\openin\@inputcheck\q@curr@file \space %
\ifeof\@inputcheck%
\@latex@warning@no@line%
{Writing file `\@currdir\@curr@file'}%
\chardef\reserved@c15 %
\ch@ck7\reserved@c\write%
\immediate\openout\reserved@c\q@curr@file\relax%
\else%
\if@filesw%
\@latex@warning@no@line%
{File `\@curr@file' already \filec@ntents@where.\MessageBreak%
Not generating it from this source}%
\let\gaborit@step@counter\@empty% <- ADDED for counting lines
\let\gaborit@count@header\@empty% <- ADDED for counting lines
\let\write\@gobbletwo%
\let\closeout\@gobble%
\else%
\edef\reserved@a{#1}%
\edef\reserved@a{\detokenize\expandafter{\reserved@a}}%
\edef\reserved@b{\detokenize\expandafter{\jobname}}%
\ifx\reserved@a\reserved@b%
\@fileswtrue%
\else%
\edef\reserved@b{\reserved@b\detokenize{.tex}}%
\ifx\reserved@a\reserved@b
\@fileswtrue%
\fi%
\fi%
\chardef\reserved@c15 %
\ch@ck7\reserved@c\write%
\if@filesw% % Foul ... trying to overwrite \jobname!
\@latex@error{Trying to overwrite `\jobname.tex'}{You can't %
write to the file you a reading from!\MessageBreak%
Data is written to screen instead.}%
\else%
\@latex@warning@no@line%
{Writing or overwriting file `\@currdir\@curr@file'}%
\immediate\openout\reserved@c#1\relax%
\fi%
\fi%
\fi%
\closein\@inputcheck%
\if@tempswa%
\gaborit@count@header% <- ADDED for counting lines
\immediate\write\reserved@c{%
\@percentchar\@percentchar\space%
\expandafter\@gobble\string\LaTeX2e file `\@curr@file'^^J%
\@percentchar\@percentchar\space generated by the %
`\@currenvir' \expandafter\@gobblefour\string\newenvironment^^J%
\@percentchar\@percentchar\space from source `\jobname' on %
\number\year/\two@digits\month/\two@digits\day.^^J%
\@percentchar\@percentchar}%
\fi%
\let\do\@makeother\dospecials%
\count@ 128\relax%
\loop%
\catcode\count@ 11\relax%
\advance\count@ \@ne%
\ifnum\count@<\@cclvi%
\repeat%
\edef\E{\@backslashchar end\string{\@currenvir\string}}%
\edef\reserved@b{%
\def\noexpand\reserved@b%
####1\E####2\E####3\relax}%
\reserved@b{%
\ifx\gaborit@reset@append##1\relax% <- ADDED for counting lines
\gaborit@reset@append% <- ADDED for counting lines
\else% <- ADDED for counting lines
\ifx\relax##3\relax%
\immediate\write\reserved@c{##1}%
\gaborit@step@counter% <- ADDED for counting lines
\else%
\edef^^M{\noexpand\end{\@currenvir}}%
\ifx\relax##1\relax%
\else%
\@latex@warning{Writing text `##1' before %
\string\end{\@currenvir}\MessageBreak as last line of \@curr@file}%
\immediate\write\reserved@c{##1}%
\gaborit@step@counter% <- ADDED for counting lines
\fi%
\ifx\relax##2\relax%
\else%
\@latex@warning{%
Ignoring text `##2' after \string\end{\@currenvir}}%
\fi%
\fi%
\fi% <- ADDED for counting lines
^^M}%
\catcode`\^^L\active%
\let\L\@undefined%
\def^^L{\expandafter\ifx\csname L\endcsname\relax\fi ^^J^^J}%
\catcode`\^^I\active%
\let\I\@undefined%
\def^^I{\expandafter\ifx\csname I\endcsname\relax\fi\space}%
\catcode`\^^M\active%
\edef^^M##1^^M{%
\noexpand\reserved@b##1\E\E\relax}%
\filecontents@append}% <- ADDED for appending
%
% Code for append
\gdef\gaborit@pre@append{%
\begingroup%
\catcode`\^^L\active%
\catcode`\^^I\active%
\catcode`\^^M\active%
\let\do\@makeother\dospecials%
\count@ 128\relax%
\loop%
\catcode\count@ 11\relax%
\advance\count@ \@ne%
\ifnum\count@<\@cclvi%
\repeat%
\let^^M\relax%
\edef\gaborit@tmpa{\FileSize{\q@curr@file}}%
\ifnum\expandafter\@car\gaborit@tmpa\@nil=0\relax%
\endgroup \def\gaborit@append{^^M\gaborit@reset@append}%
\else%
\everyeof{\noexpand}%
\edef\gaborit@tmpa{\@@input\q@curr@file \space}%
\edef\x{\endgroup%
\edef\noexpand\gaborit@append{%
\noexpand\gaborit@trim@EOF\gaborit@tmpa\noexpand\gaborit@EOF}}\x%
\fi%
\filec@ntents@overwrite%
\filec@ntents@noheader}%
\gdef\gaborit@trim@EOF#1^^M\gaborit@EOF{^^M#1%
^^M\noexpand\gaborit@reset@append}%
\endgroup%
\def\filec@ntents@append{%
\let\filecontents@preappend\gaborit@pre@append
\def\filecontents@append{\gaborit@append}}
\let\filecontents@preappend\@empty
\let\filecontents@append\@empty
%
% For counting lines
\newcounter{FC@total@lines}
\newcounter{FC@lines}
\def\gaborit@reset@counter{\setcounter{FC@total@lines}{0}}
\def\gaborit@reset@append{\setcounter{FC@lines}{0}}
\def\gaborit@step@counter{%
\stepcounter{FC@total@lines}%
\stepcounter{FC@lines}}
\def\gaborit@count@header{%
\addtocounter{FC@total@lines}{4}%
\addtocounter{FC@lines}{4}} % Number of lines in the header
\makeatother
\begin{filecontents}[overwrite]{testfile.tex}
a
b
c
\end{filecontents}
\typeout{\arabic{FC@total@lines} lines written; \arabic{FC@lines} appended}
\begin{filecontents}[append]{testfile.tex}
d
e
f
g
\end{filecontents}
\typeout{\arabic{FC@total@lines} lines written; \arabic{FC@lines} appended}
\begin{document}
\end{document}
Al ejecutar este código, TeX imprime en la terminal:
LaTeX Warning: Writing or overwriting file `./testfile.tex'.
7 lines written; 7 appended
(./testfile.tex)
LaTeX Warning: Writing or overwriting file `./testfile.tex'.
11 lines written; 4 appended
( FC@total@lines
contiene el número total de líneas en el archivo y el FC@lines
contador solo contiene el número de líneas agregadas) y el archivo testfile.tex
contiene:
%% LaTeX2e file `testfile.tex'
%% generated by the `filecontents' environment
%% from source `test' on 2019/12/20.
%%
a
b
c
d
e
f
g
Como notaste, cuando lo haces:
\begin{filecontents*}{testfile.tex}
\end{filecontents*}
%
\begin{filecontents}[append]{testfile.tex}
a
\end{filecontents}
(es decir, no escriba nada y luego agregue una línea con a
) el archivo que obtiene es:
a
Esto se debe a que cuando TeX lee un archivo vacío, lo trata como si fuera un archivo con una sola línea vacía 1 , por lo que no se puede diferenciar fácilmente entre esos dos casos y hay que elegir el mal menor: o tener un archivo vacío espurio. línea al principio del archivo cuando se agrega a un archivo vacío, o eliminar una línea vacía cuando se escribe en un archivo que contiene una sola línea vacía.
Sin embargo, con las utilidades de archivos introducidas en pdfTeX (pero también disponibles en otros motores hoy en día) puedes consultar el tamaño (en bytes) de un archivo, por lo que puedes tener un comportamiento diferente para un archivo vacío. En el código utilicé expl3
's \file_size:n
para evitar tener que lidiar con archivos inexistentes.
1. Puedes ver esto con este archivo de prueba:
\catcode`\@=11
\newwrite\test
\def\testit#1{\immediate\openout\test \jobname.testfile\relax #1%
{\catcode`\^^M=13 \everyeof{\noexpand}%
\edef\tmpa{\ifdefined\@@input \@@input \else \input \fi \jobname.testfile }\show\tmpa}%
\immediate\closeout\test}%
\testit{}% empty file
\testit{\immediate\write\test{}}% single empty line
\csname stop\endcsname\bye
Creo que este comportamiento se debe a cómo TeX agrega un \endlinechar
al final de cada línea. Cuando lee un archivo vacío, todavía lee una línea (incluso si está vacía) y luego inserta \endlinechar
( ^^M
) y esto hace que parezca como si el archivo tuviera una sola línea vacía.