I'm trying to find a way to change my master/child document workflow in LaTeX using the package "subfiles", so I'm trying to append and prepend to every file I already have the following lines:
Lines to prepend:
\documentclass[<mainfile>]{subfiles}
\begin{document}
Line to appen:
\end{document}
I was thinking of using bash but I could be nice too with Python, I don't know what would be best.
If you have any suggestion :-) ?
An intuitive way is to use cat
For example, first create two files prepend.tex
and append.tex
that contain the content you want to add. Concatenate them with the source file to create the desired target file
$ cat prepend.tex source.tex append.tex > target.tex
To apply it recursively to all existing files inside a directory say src/
you can use:
$ find src/ -type f -name "*.tex" -exec sh -c "cat prepend.tex '{}' append.tex > '{}'.tmp" \;
This will create a new .tmp
file alongside each source file. Make sure that the result in those .tmp
files are exaclty what you want before proceeding to overwrite the source files with the .tmp
ones:
$ find src/ -type f -name "*.tmp" -exec rename -n 's/.tmp$//' '{}' \;
change the option from -n
to -f
of the rename
command to force overwrite