Generalhow to produce multiple files in one step?

General information and discussion about TeXnicCenter
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

how to produce multiple files in one step?

Post by ptoche »

How can I compile multiple input and produce multiple output?

Suppose I have many files, file1.tex, file2.tex,..., filen.tex. I would like to compile them to obtain file1.ps, file2.ps, ..., filen.ps. How can I do this in a minimum number of steps?

Currently I would compile my n files separately by opening them in TeXNicCenter and running my favorite build routine *one at a time*. Because I am using the same master file to compile the n files, I then have to rename each file manually. Tedious task for n>10, not to mention my current n>50. (each file.ps is a picture produced using pstricks and to be later inserted into a LaTeX article, every time I update my source pictures I then want to re-run pstricks on all the pictures).

What I'm looking for, if available, is a way to have one master file, master.tex in which I write something like:

\myinput{file1}
\myinput{file2}
...
\myinput{filen}

to be compiled once only, where the command \myinput means that TeXNicCenter will compile with \input{file1} first and create a file named file1.ps, then compile with \input{file2} and create a file named file2.ps, and so on all the way to filen.ps

maybe I want something like a LaTeX-interpretable loop.

can or cannot?

many thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: how to produce multiple files in one step?

Post by josephwright »

A few ways suggest themselves, but a bit more detail would be useful. What is the relationship between the master file and the various sub-files? Could we perhaps have an example of one sub-file and enough of the master file to compile.

The obvious methods are:
1) Use \write18 to do a sub-compilation for each file
2) Write a batch file do do things automatically (no TeX programming at all!)
Joseph Wright
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

great news

Post by ptoche »

Thanks a lot for your reply Joseph, so there are solutions: great news.

I have used batch files before, but have never written my own. I'm afraid I'm rather ignorant in these matters.

So let me detail what I'd like to achieve. I run simulations (usually in matlab and maple) that produce ten, twenty or more figures that I then import into my LaTeX documents (article and especially unpublished appendix). Once in a while I decide to change some parameter value or the name of a variable or such which affects all or most of the figures. It takes seconds to produce the raw figures with my simulation software: I would like to add my pstricks labels to the whole batch of new figures in as few steps as feasible. Running a batch file is probably a good idea.

Here is what I am doing at present. My master file is named FiguresMake.tex and contains the following:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{mathrsfs}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{textcomp}
\usepackage{mathptmx}% use mathptmx for Times
\usepackage[scaled=0.92]{helvet}% 92% is suitable with Times
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{pstricks}
\pagestyle{empty}

\begin{document}
\input{FigureMake1}
%\input{FigureMake2}
%\input{FigureMake3}
%\input{FigureMake4}
%\input{FigureMake5}
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The slave file named FigureMake1.tex contains something like

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
\begin{pspicture}(0,0)(\textwidth,\textheight)
\rput[lb](0,0){\includegraphics[width=\textwidth]{FigureRaw1.ps}}
\rput[rb](11.8,0.1){\Large{${\bf{t}}$}}
\rput[ct](0.5,11.7){\Large{${\bf{D(t)}}$}}
%\psgrid[gridcolor=blue,griddots=10,subgriddiv=2,subgriddots=10,subgridcolor=gray,gridlabelcolor=blue,gridlabels=7pt]
\end{pspicture}
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

where FigureRaw1.ps is a graphic file created by some mathematical software, postscript (usually encapsulated).

The current directory holds a bunch of these FigureRaw1.ps, FigureRaw2.ps, etc.

With TeXNicCenter (patiently but painfully, and sometimes not so patiently if truth be told) I comment and uncomment each line in my Master file (\input{FigureMake1} then %\input{FigureMake1}) so as to produce the postscript file associated with it. Each time I run a build I have named psbuild and which does:

1) Compiler:
C:\Program Files\MiKTeX 2.7\miktex\bin\latex.exe
--src -interaction=nonstopmode "%Wm"

2) Postprocessor:
C:\Program Files\MiKTeX 2.7\miktex\bin\dvips.exe
-P pdf -G0 "%Bm.dvi"

Each time, this creates a file named FigureMake.ps. I then, one by one, rename FigureMake.ps into FigureFinal1.ps, FigureFinal2.ps, etc.

I'd like to automate this tedious process.

By the way, if you find any obsolete or absurd line of code in what I wrote, don't hesitate to point it out.

So, batch file the way to go? ANY pointer on the art/science of batch file coding will be much appreciated.

Thanks Joseph,

Patrick.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

how to produce multiple files in one step?

Post by T3. »

Here's one possible solution with a batch file:

Code: Select all

@echo off
rem latexify-figs.bat
echo Batch compilation of figures
echo Usage: latexify-figs.bat file1.tex file2.tex ...
echo You can also use wildcards, e.g. latexify-figs.bat file*.tex

for %%I in (%*) do call :onefig %%I
goto :eof

:onefig figfilename
copy /y %1 FigureMake0
latex FiguresMake && dvips -E -o "%~dpn1-Final.eps" FiguresMake
In your template file 'FiguresMake' you only need one '\input{FigureMake0}' and then call this batch with tex files you want to compile as arguments (you can use wildcards). Output .eps files will have a '-Final' suffix added (change it if you like). Substitution of 'Make' to 'Final' in the file name can also be made, let me know if you really want it.

Alternatively, if you prefer to specify the files to be compiled directly in the batch file, change the for loop in the above to:

Code: Select all

for %%I in (
FigureMake1.tex
FigureMake2.tex
FigureMake3.tex
) do call :onefig %%I
Cheers,

Tomek
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Re: how to produce multiple files in one step?

Post by ptoche »

Tomek: thanks a lot for your help, I appreciate greatly.

I have followed your steps, but things haven't gone as smoothly as hoped. Perhaps I am overlooking something.

I have tested two versions of the batch file. The first version (the first code you gave) produces nothing. The second version (your alternative version) produces an error message.

Your alternative code is the one I really need (as my files are not named FiguresMaken.tex, but have non-systematic, descriptive names instead). So let me describes what happens when I run your alternative code.

I have all the relevant files in one directory: the Master file containing only \input{FiguresMake0}) and named FiguresMake.tex, together with the figure-making files FiguresMake1.tex, FiguresMake2.tex, FiguresMake3.tex, and of course together with the ps graphics files called therein.

When I double-click on the batch file, a window opens with the following error message "LaTeX Error: File `FiguresMake0.tex´ not found"

If I create a file named FiguresMake0.tex, then the loop runs, but what it does is create several identical copies of the eps associated with FiguresMake0.tex. The output files do have the expected distinct names: FiguresMake1-Final.eps, FiguresMake2-Final.eps, FiguresMake3-Final.eps. There is also created a file named FiguresMake.ps containing the same image but slightly smaller (49k smaller). In summary, the call to FiguresMake1.tex, FiguresMake2.tex and FiguresMake3.tex contained in:

for %%I in (
FiguresMake1.tex
FiguresMake2.tex
FiguresMake3.tex
)

is not processed and instead the call to FiguresMake0.tex is processed 3 (or 4) times.

Unfortunately I do not know if this results from a mistake of mine in reproducing the steps you have outlined above or if some adjustment to the code is required. Have you tested the code yourself?

many thanks!
Patrick.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

how to produce multiple files in one step?

Post by T3. »

From what you describe it looks as if everything works except for the copy statement. I don't know why. Do you have any spaces in the file names? In that case they have to be in double quotes. Try this batch below. It should be more robust and report errors. Make sure that the file names in the for loop match those in the directory with the batch (you used FiguesMake?.tex in your reply, while in my original code it was FigureMake?.tex, mind the missing 's').

Code: Select all

@echo off
rem latexify-figs.bat
echo Batch compilation of figures

setlocal
cd /d %~dp0
for %%I in (
"FigureMake1.tex"
"FigureMake2.tex"
"FigureMake3.tex"
) do call :onefig %%I
pause
goto :eof

:onefig figfilename
if not exist "%~f1" goto :nofile
copy /y "%~f1" FigureMake0 
if not errorlevel 1 latex FiguresMake
if not errorlevel 1 dvips -E -o "%~dpn1-Final.eps" FiguresMake
goto :eof

:nofile
echo File not found: "%~f1"
goto :eof
Cheers,

Tomek
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

how to produce multiple files in one step?

Post by ptoche »

Thanks Tomek, I really appreciate your taking so much time over this.

There is no space in the file names. In fact I was using exactly the names you had suggested. I got mixed up with Figure and Figures in typing up my message, but I was actually doing the correct thing on my pc. The new code suffers from the same problem.

If I remove the line that says:

Code: Select all

copy /y "%~f1" FigureMake0 
I also get the behaviour reported earlier -- three eps files are created but they are all identical to the file called in the Master file.

So it seems to me that the batch file is executing the loop properly, but it doesn't understand that the \input{FigureMake0} call that resides in the Master file must be changed to \input{FigureMake1}, and then to \input{FigureMake2}, and so on.

So you must be correct that the problem is with the line where the "copy" statement is written. If I replace FigureMake0 by any name the batch file would compile that name 3 times.

Could this problem be related to the fact that the code was initially written for a loop over i in FigureMakei but then changed to arbitrary names? Does the batch code understand that \input{FigureMake0} is to be changed to \input{FigureMake1}?

And I'm too ignorant to understand what's going wrong! So thanks Tomek, if you find a fix that would be great; at any rate, I'm very grateful for your guidance.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Re: how to produce multiple files in one step?

Post by T3. »

What system do you use? I tried to compile with latex a file without extension on Vista and it failed with the message that it could not be found :? . I just tried the same on XP and it works (as it should). This is strange enough that I will investigate it further.

Anyway, maybe this missing extension is the culprit. Try 'copy /y "%~f1" FigureMake0.tex', so that FigureMake0 will have a .tex extension, maybe that will help. Other than that I've run of ideas for the moment. I do a fair bit of batch programming and I really don't see what could be wrong with that batch. But I will find out, just not in the coming few days, because I'm busy with other things.

Cheers,

Tomek
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

how to produce multiple files in one step?

Post by ptoche »

You got it Tomek!

Yes I'm using vista.

Yes adding the .tex extension was sufficient. The batch file works now.

Thank you very, very much. This is a lifesaver! I'm off to batchproduce now.

To sum up, below the code that worked:

Code: Select all

@echo off
rem latexify-figs.bat
echo Batch compilation of figures

setlocal
cd /d %~dp0
for %%I in (
"FigureMake1.tex"
"FigureMake2.tex"
"FigureMake3.tex"
) do call :onefig %%I
pause
goto :eof

:onefig figfilename
if not exist "%~f1" goto :nofile
copy /y "%~f1" FigureMake0.tex
if not errorlevel 1 latex FiguresMake
if not errorlevel 1 dvips -E -o "%~dpn1-Final.eps" FiguresMake
goto :eof

:nofile
echo File not found: "%~f1"
goto :eof
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Re: how to produce multiple files in one step?

Post by ptoche »

okay, I get it: the file named Fig0.tex is just a temporary file. I do not need to create a file of that name, it will be created automatically in the process (I may as well name it tmp.tex). I get it.

Now Tomek, this is extremely useful and, again, thanks a lot.

Since I have you on the line, may I ask you one more thing. The compilation produces the following files: the said Fig0.tex, and the usual .aux, .log and .dvi extensions of the Master file.

Would it be easy to insert a line in the batch file to delete them after the compilation is complete? (since I will only need the .eps files)

It's probably something I should be able to figure out by myself mind you: where would you recommend I look for information on batch files related to LaTeX, do you know any good url on that?

many thanks,

Patrick.
Post Reply