Skip to content

Instantly share code, notes, and snippets.

@ptoche
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptoche/8854313 to your computer and use it in GitHub Desktop.
Save ptoche/8854313 to your computer and use it in GitHub Desktop.
compile a latex image with pdflatex, convert to png, crop it, and clean up the temp files.
% !TeX document-id = {cf6594ac-97a2-46b5-af4f-f9d9c8aaa47d}
% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\documentclass{article}%
\usepackage{filecontents}%
\begin{filecontents*}{tmp.tex}%
\input{tikz-board}%
\end{filecontents*}
%%
%% compile pdf
\immediate\write18{pdflatex tmp}
%%
%% convert pdf to png
%% convert etc: -density 3600 almost good
%\immediate\write18{convert -density 3600 -alpha on tmp.pdf tmp.png}%
%% gs etc: -r10240 too high?
\immediate\write18{gs -dBATCH -dNOPAUSE -sDEVICE=pnmraw -r5120 -sOutputFile=tmp.png tmp.pdf}%
%% more options to play with:
%\immediate\write18{gs -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=pnmraw -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 -r5120 -sOutputFile=tmp.png tmp.pdf}%
%%
%% crop the png
\immediate\write18{convert -trim tmp.png fig.png}
%% multi-platform commands used to clean up
%% dos: del, unix:rm
%% dos: ren, unix:mv
\usepackage{ifplatform}%
\ifwindows% dos
\providecommand\DeleteFile[1]{\immediate\write18{del #1}}
\providecommand\RenameFile[1]{\immediate\write18{ren #1}}
\else% unix
\providecommand\DeleteFile[1]{\immediate\write18{rm #1}}
\providecommand\RenameFile[1]{\immediate\write18{mv -f #1}}
\fi%
\begin{document}%
%% remove auxiliary files
\DeleteFile{tmp.aux}
\DeleteFile{tmp.log}
\DeleteFile{tmp.tex}
\DeleteFile{tmp.png}
\DeleteFile{\jobname.log}
\DeleteFile{\jobname.aux}
\RenameFile{tmp.pdf fig.pdf}
%
This useless document is created in order to avoid an error message and may safely be deleted.
Useful Reference:
%http://stackoverflow.com/questions/977540/convert-a-pdf-to-a-transparent-png-with-ghostscript
\end{document}%
\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{shapes.geometric}% polygon, ellipse, star, diamond, etc.
\usetikzlibrary{shapes.symbols}% starburst
\usetikzlibrary{shadows}
\usetikzlibrary{shadings}
% Needs development version of PGF/TikZ
% Unpack pgf_CVS.ds.zip inside: Users/UserName/Library/texlive/2013/texmf-var
% Set Grid Dimensions: Rectangular Shape
\newif\iftiny\tinyfalse
\iftiny
\newcommand{\na}{1}% first number
\newcommand{\nx}{3}% number along x-axis
\newcommand{\ny}{3}% number along y-axis
\fi
\newif\ifbig\bigfalse
\ifbig
\newcommand{\na}{1}% first number
\newcommand{\nx}{10}% number along x-axis
\newcommand{\ny}{10}% number along y-axis
\fi
\newif\ifsmall\smallfalse
\ifsmall
\newcommand{\na}{1}% first number
\newcommand{\nx}{4}% number along x-axis
\newcommand{\ny}{4}% number along y-axis
\fi
\newif\ifmedium\mediumfalse
\ifmedium
\newcommand{\na}{1}% first number
\newcommand{\nx}{5}% number along x-axis
\newcommand{\ny}{5}% number along y-axis
\fi
\newif\ifcurrent\currenttrue
\ifcurrent
\newcommand{\na}{13}% first number
\newcommand{\nx}{4}% number along x-axis
\newcommand{\ny}{4}% number along y-axis
\fi
\pgfmathsetmacro{\nb}{\na+\nx*\ny-1}% last number: based on first number + grid
\newcommand{\nd}{0}% 0 or 1 to change direction, standard v. boustrophédon
\newcommand{\nn}{0.8}% tweak scale to control white space in between cells
\newcommand{\ns}{101}% special number, irregular star shape, for board games
\tikzset{%
set color/.style = {
fill = #1,
draw = #1!50!black,
},
every number/.style = {
opacity = 0.9,
drop shadow,
text = white,
font = \fontfamily{pzc}\selectfont,
align = center,
scale = \nn
},
every even number/.style = {
set color = blue!70!black,
shape = circle,
inner sep = 1ex
},
every odd number/.style = {
set color = blue!70!black,
shape = diamond,
inner sep = 1.2ex
},
every prime number/.style = {
set color = red!70!black,
shape = diamond,
inner sep = 1.2ex
},
number 2/.style = {
set color = red!70!black,
shape = circle,
inner sep = 1ex
},
number special \ns/.style = {
set color = green!50!blue,
shape = starburst,
starburst points = 7,
drop shadow,
inner sep = 1ex
}
}%
\begin{document}
\begin{tikzpicture}
\coordinate(last);
\foreach \n [evaluate={%
% \style: \p for prime, \e for even
% x ? y : z means "if x then y else z"
\p = isprime(\n);
\e = mod(\n,2)==0;
\style = \p ? "prime" : (\e ? "even" : "odd");
\y=-floor((\n-1)/\ny+\nd);
\k=mod(-\y,2);
\x=(\ny-1)*\k-(\k*2-1)*mod(\n-1,\ny);
}] in {\na,...,\nb}{%
\coordinate(new) at (\x,\y);
\node[every number/.try, every \style\space number/.try, number special \n/.try, number \n/.try, minimum size=1cm](last) at (\x,\y) {\n};
}%
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment