# crop a tikzpicture

My purpose is to create a table of numbers in a fancy font. My approach is based on tikzpicture: I plan to first generate a bunch of images of the numbers I want and later insert them into a master document with \includegraphics

At this time my images have too much whitespace. This problem has come up before on tex.stackexchange, but I haven't been able to make the proposed solutions work in this case. Here is what I am getting:

This is based on a suggestion found here:

Creating Logo with Fancy Font

and this is the code:

\PassOptionsToPackage{svgnames,x11names}{xcolor}
\documentclass[tikz,margin=0pt]{standalone}
\begin{document}%
\fontfamily{pzc}\selectfont%
\begin{tikzpicture}%
\node[anchor=base, inner sep=0pt, outer sep=0pt, text=Brown4!30!DarkBlue,scale=50]{1};%
\end{tikzpicture}%
\end{document}%


I set the inner sep and outer sep  to 0pt, but that does not appear to be enough.

Suggestions welcome!

• I still thing that standalone only cuts to where no "points" are placed, even though they are invisible. An easy solution is to use Imagemagick to further crop it: convert -trim [-density 300|400] in.png out.png – zeroth Jan 24 '14 at 10:51
• Thanks zeroth! I have imagemagick and also have command-line solutions involving pdfcrop and also based on postscript and ghostscript, but I was looking for a tikzpicture solution, if there is one. – PatrickT Jan 24 '14 at 10:56
• I think the problem is with the font itself. You are not drawing it with TiKZ. In fact if you add draw to your node will see that standalone crops it correctly. – Ignasi Jan 24 '14 at 13:16
• Why not typesetting the character directly? – egreg Jan 24 '14 at 13:46

## 2 Answers

As noted by ignasi, the problem is with the font itself:

\documentclass{article}
\begin{document}%
\fboxsep=0pt
\fontfamily{pzc}\selectfont%
\fbox{1}
\end{document}%


Produces this:

Compare above with this:

\documentclass{article}
\begin{document}%
\fboxsep=0pt
%\fontfamily{pzc}\selectfont%
\fbox{1}
\end{document}%


There is always space on left and right of 1. This tells us that standalone is doing good job in cutting the borders and the space left empty is not the border actually.

Hence your best bet will be to use imagemagick. Now the question is making the process automatic. You have two ways: 1. do it from within the .tex file, and 2. use a batch (.bat for windows) file:

# From within the .tex file

(Compile this with pdflatex --shell-escape filename.tex) :

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{1.tex}
\PassOptionsToPackage{svgnames,x11names}{xcolor}
\documentclass[tikz,margin=0pt]{standalone}
\begin{document}%
\fontfamily{pzc}\selectfont%
\begin{tikzpicture}%
\node[anchor=base, inner sep=0pt, outer sep=0pt, text=Brown4!30!DarkBlue,scale=50]{1};%
\end{tikzpicture}%
\end{document}%
\end{filecontents*}
%%
%% Produce pdf file
\immediate\write18{pdflatex 1}
%%
%% get png file from pdf
\immediate\write18{convert -density 300 -alpha on 1.pdf 1.png}
%%
%% crop the png file
\immediate\write18{convert -trim  1.png 1-trimmed.png}
%
\begin{document}
See the same folder as this file.
\end{document}


## The batch file:

Save the following as a file mytrim.bat:

@ECHO ON
cls
REM ECHO.

REM convert pdf to png

CD /D %~dp0
SET Program="convert.exe"
REM convert pdf to png
for %%A in (*.pdf) do %Program%  -density 300 -alpha on %%A %%~nA.png
mkdir pngs
REM convert png to trimmed png
for %%A in (*.png) do %Program%  -trim %%A pngs/%%~nA-trimmed.png
REM delete temporary un-trimmed png files
del *.png
Pause


First produce all numbers in pdf form in a folder. Put mytrim.bat in the same folder and double click on it. A new folder named pngs will be created which would contain trimmed png files.

• this is ever so thorough and I am most grateful! – PatrickT Jan 25 '14 at 5:01
• sorry to bother you, now that I'm in front of my computer again I have a question: how do you run the .tex solution? (latex, pdflatex, something else?) – PatrickT Jan 25 '14 at 17:34
• I tried latex and pdflatex with shell-escape, but I get runsystem(pdflatex 1)...disabled (restricted) in the log file. (I have no problem with the .bat file on Windows) I'm on Mac OSX, I installed imagemagick from http://cactuslab.com/imagemagick/. Any ideas? Should I post a separate question? – PatrickT Jan 25 '14 at 19:29
• @PatrickT Sorry for the delay. I used pdflatex --shell-escape filename.tex. It needs shell escape (or write18). I added it to the answer. – user11232 Jan 25 '14 at 23:26
• Thanks Harish, I suspect my texlive is not speaking to imagemagick, but no worries I have it set up on Windows, just need to switch machines. Thanks very much! – PatrickT Jan 26 '14 at 6:58

If I understood correctly, clipping seems to work:

\PassOptionsToPackage{svgnames,x11names}{xcolor}
\documentclass[tikz,margin=0pt]{standalone}
\begin{document}%
\fontfamily{pzc}\selectfont%
\begin{tikzpicture}%
\clip (-2.45,0) rectangle (3.75,10.25);
\node[anchor=base, inner sep=0pt, outer sep=0pt,text=Brown4!30!DarkBlue,scale=50]{1};%
\end{tikzpicture}%
\end{document}%

• Yes, it works. But I guess that he wants an automatic way since he will make similar to other numbers. In your case a manual setup would be necessary. – Sigur Jan 24 '14 at 12:22
• @user44524, this is not a bad answer at all and I'm upvoting, as I hadn't been able to make \clip work, so your solution will prove useful no doubt, thanks! – PatrickT Jan 25 '14 at 5:00