# TikZ to PDF from the command line

I am generating TikZ files using figures from MATLAB (using a MATLAB function from the stack exchange). I then open these files using TikzEdt, and generate the PDF file. Is there a way to make it automatically, that is to say, using some commands, generate the PDF file?

-
Which operating system do you have, Windows, Linux, OS X? Edit: Oh, and do you mean matlab2tikz? –  Torbjørn T. Oct 1 '13 at 9:50
I am in Windows, and yes the function is matlab2tikz –  user2536125 Oct 6 '13 at 8:59

# Updated answer -- standalone

(Note: I haven't tested this updated part of the answer, with the standalone option. Will remove this message when I have.)

matlab2tikz has an option standalone that generates a complete, compilable file with the standalone documentclass. Hence, by doing

matlab2tikz('test.tikz','standalone',true)


the resulting file can be compiled directly, so on the command line one can do

pdflatex test.tikz


If you want to do this with all .tikz files in a folder, you can loop over all the files. This can be done directly in Matlab, or you can do it via the command line. Below is an example for bash. It may however be more convenient to do this on a per plot-basis, so you could define a simple Matlab-function that does this for you.

## Via Matlab

### Function

Save the following as plotviatikztopdf.m:

function plotviatikztopdf(filename,varargin)
% Generate a LaTeX file using matlab2tikz and compile
% this directly to PDF with pdflatex, cleaning up temporary
% files after compilation.
%
%    PLOTVIATIKZTOPDF(FILENAME,VARARGIN)
% FILENAME is the name of the generated LaTeX file, with file ending,
% e.g. THING.TEX. VARARGIN is (optional) arguments to matlab2tikz,
% such as definitions of width and height.

% strip file ending
ind = find(filename=='.',1,'last');
basename = filename(1:ind-1);

% run matlab2tikz
matlab2tikz(filename,'standalone',true,varargin{:})

% run pdflatex on generated file
command = sprintf('pdflatex %s', filename);
system(command);

% remove .aux and .log files
if ispc
system(sprintf('del "%s.aux"',basename));
system(sprintf('del "%s.log"',basename));
elseif isunix
system(sprintf('rm %s.aux %s.log',basename,basename));
end


After creating your plot, execute

plotviatikztopdf('mylovelyplot.tikz')


to save the generate the .tikz file and convert it to PDF. You can also specify the width and height (as specific lengths, not commands), e.g.

plotviatikztopdf('mylovelyplot.tikz','width','10cm','height','10cm')


### All files in folder

You can actually do this from within Matlab itself, at least it worked for my simple test case. I tested this in a Linux environment, if Matlab handles things differently in other operating systems it may not work. (This isn't very pretty.)

cd /path/to/tikz/files

files = dir('*.tikz');

for k = 1:length(files)
ind = find(files(k).name=='.',1,'last');
basename = files(k).name(1:ind-1);
if ispc
command = sprintf('pdflatex %s', filename);
elseif isunix
command = sprintf('pdflatex %s', filename);
end
system(command);
end


## Terminal

If you are on Linux (probably works in OS X as well) you can try the following shell script. It uses the same idea as above (assuming you are in the correct folder):

for f in *.tikz
do
jn=$(basename$f .tikz)
pdflatex $f rm$jn.aux $jn.log done  Save it as e.g. runtikz.sh, make it executable with chmod u+x runtikz.sh, and run it with ./runtikz.sh in the terminal. # Old answer I assume you use matlab2tikz to generate the .tikz files. This generates a tikzpicture with an axis environment from pgfplots, so the minimal LaTeX code needed to compile it is usually \documentclass{standalone} \usepackage{pgfplots,amsmath} \begin{document} \input{file.tikz} \end{document}  Assuming no libraries are required, and you haven't specified width/height as commands, as suggested by matlab2tikz. amsmath is needed because matlab2tikz uses \text from amsmath (or amstext) for axis labels. Now, you can write code directly as an argument to pdflatex on the command line, so you can do pdflatex -jobname test "\documentclass{standalone}\usepackage{pgfplots,amsmath}\begin{document}\input{test.tikz}\end{document}"  If you want to do this with all .tikz files in a folder, you can loop over all the files. This can be done directly in Matlab, or you can do it via the command line. Below is an example for bash. It may however be more convenient to do this on a per plot-basis, so you could define a simple Matlab-function that does this for you. ## Via Matlab ### Function Save the following as plotviatikztopdf.m: function plotviatikztopdf(filename,varargin) matlab2tikz(filename,varargin{:}) ind = find(filename=='.',1,'last'); basename = filename(1:ind-1); if ispc command = sprintf('pdflatex -jobname %s "\\documentclass{standalone}\\usepackage{pgfplots,amsmath}\\begin{document}\\input{%s}\\end{document}"',basename,filename); elseif isunix command = sprintf('pdflatex -jobname %s "\\\\documentclass{standalone}\\\\usepackage{pgfplots,amsmath}\\\\begin{document}\\\\input{%s}\\\\end{document}"',basename,filename); end system(command); if ispc system(sprintf('del "%s.aux"',basename)); system(sprintf('del "%s.log"',basename)); elseif isunix system(sprintf('rm %s.aux %s.log',basename,basename)); end  There is a difference in how Windows and Linux handles backslashes in a command line, I guess because Windows uses the backslash to separate directories. When I did this in Linux I needed to send the string \\documentclass{standalone}... to the terminal via system, and to print a backslash in the format string with sprintf you have to escape it with a second backslash (sprintf('\ab') will just print b, sprintf('\\ab') will print \ab). That is the reason for the quadruple backslashes seen above. In Windows however, there is no need to handle backslashes in the command any special way, so the command string can be \documentclass..., so you just need two backslashes. In the above function I added a check for OS, and it worked fine on my small test on a Windows machine. After creating your plot, execute plotviatikztopdf('mylovelyplot.tikz')  to save the generate the .tikz file and convert it to PDF. You can also specify the width and height (as specific lengths, not commands), e.g. plotviatikztopdf('mylovelyplot.tikz','width','10cm','height','10cm')  ### All files in folder You can actually do this from within Matlab itself, at least it worked for my simple test case. I tested this in a Linux environment, if Matlab handles things differently in other operating systems it may not work. (This isn't very pretty.) cd /path/to/tikz/files files = dir('*.tikz'); for k = 1:length(files) ind = find(files(k).name=='.',1,'last'); basename = files(k).name(1:ind-1); if ispc command = sprintf('pdflatex -jobname %s "\\documentclass{standalone}\\usepackage{pgfplots,amsmath}\\begin{document}\\input{%s}\\end{document}"',basename,filename); elseif isunix command = sprintf('pdflatex -jobname %s \\\\documentclass{standalone}\\\\usepackage{pgfplots,amsmath}\\\\begin{document}\\\\input{%s}\\\\end{document}',basename,filename); end system(command); end  ## Terminal If you are on Linux (probably works in OS X as well) you can try the following shell script. It uses the same idea as above (assuming you are in the correct folder): for f in *.tikz do jn=$(basename $f .tikz) pdflatex -jobname$jn "\documentclass{standalone}\usepackage{pgfplots,amsmath}\begin{document}\input{$f}\end{document}" rm$jn.aux \$jn.log
done


Save it as e.g. runtikz.sh, make it executable with chmod u+x runtikz.sh, and run it with ./runtikz.sh in the terminal.

-
When I execute the command pdflatex -jobname test "\documentclass{standalone}\usepackage{pgfplots}\begin{document}\input{SuddenAre‌​aChangeFlowChart.tikz}\end{document}" I get the following error in Matlab: <Undefined function 'pdflatex' for input arguments of type 'char'.> Can any one give a suggestion please? –  user2536125 Oct 6 '13 at 8:59
Perhaps I was unclear. Try writing a ! before the command, to run external programs from the Matlab terminal you have to "escape" to the system terminal with a !. Did you try the function I defined? –  Torbjørn T. Oct 6 '13 at 9:45
The ! did work (partially), and it generated only the *.aux and .*log files then stoped... Regarding the function that you suggested, it executes without error but no pdf is produced. here is the output: >> plotviatikztopdf('SuddenAreaChangeFlowChart.tikz') This is pdfTeX, Version 3.1415926-2.3-1.40.12 (MiKTeX 2.9) pdflatex: Windows API error 53: The network path was not found. pdflatex: Data: \\documentclass{standalone}\\usepackage{pgfplots}\\begin{document}\\input{SuddenArea‌​ChangeFlowChart.tikz}\\end{document}.tex –  user2536125 Oct 6 '13 at 10:01
@user2536125 Ok. I'll get access to a computer with Windows 7, MikTeX and Matlab tomorrow, so I'll test it then. As mentioned I only tested this on a Linux system, and there it worked fine, could be some differences between operating systems. But I'll check back tomorrow. You could also test to do the same from a command prompt, not from within Matlab. –  Torbjørn T. Oct 6 '13 at 10:07
T Ok, will do and let you know. –  user2536125 Oct 6 '13 at 10:22
show 7 more comments