You have empty lines between the document
and tikzpicture
environment which puts the picture in an paragraph (which is \textwidth
wide). Simply removing the lines fixes this:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [step=0.5] (-1.4,-1.4) grid (1.4,1.4);
\end{tikzpicture}
\end{document}
The rest of your question is already answered in
Compile a LaTeX document into a PNG image that's as short as possible.
In short:
pdflatex file
convert -density 300 file.pdf -quality 90 file.png
or with v1.0 of the standalone
class:
\documentclass[convert={density=300,size=1080x800,outext=.png}]{standalone}
compile with:
pdflatex -shell-escape file
You might also want to use the new border
option to set the border to 0pt
.
The order of preference when files with the same name and different extensions is
.png .pdf .jpg .mps .jpeg .jbig2 .jb2 .PNG .PDF .JPG .JPEG .JBIG2 .JB2
which is stored in the macro \Gin@extensions
. So if you have both image.png
and image.pdf
, pdflatex
will load the former.
If you are mixing case in extensions, then
\DeclareGraphicsExtensions{%
.png,.PNG,%
.pdf,.PDF,%
.jpg,.mps,.jpeg,.jbig2,.jb2,.JPG,.JPEG,.JBIG2,.JB2}
will ensure that PNG are always preferred over PDF files. For the final version it will be sufficient to switch the two lines.
A handier way, suggested by Heiko Oberdiek, is to use the package grfext
:
\usepackage{grfext}
\PrependGraphicsExtensions*{.png,.PNG}
that will have the same effect without the need to check in pdftex.def
for the list of extensions.
If you want also automatic conversion, you can say
\usepackage{graphicx}
\usepackage{epstopdf}
\epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{convert #1 \OutputFile}
\DeclareGraphicsExtensions{%
.png,.PNG,%
.pdf,.PDF,%
.jpg,.mps,.jpeg,.jbig2,.jb2,.JPG,.JPEG,.JBIG2,.JB2}
When image.pdf
exists but not image.png
, the file image-pdf-converted-to.png
will be created and loaded in its place. Add the options you prefer between convert
and #1
(for example -density 100
or something like that).
You need to call pdflatex
with the --shell-escape
option for this automatic conversion to work. Of course you'll comment out the \epstopdfDeclareGraphicsRule
command for the final version, when only PDF files should be loaded (and switch the order of precedence in the lines below).
Best Answer
The package manual states that “the following default settings are used: PNG format, a density of 300dpi, no explicit size […].”
So there's no surprise that
density=300
doesn't result in a different output.I wouldn't trust the “file properties” of the resulting file. How should a picture viewer (or your OS) know what an inch is? It's just pixels at the output side now.
Consider the following example where I typeset a black 1 inch by 1 inch square.
The output
.png
is 300 pixels by 300 pixels.Code
Output
To complement egreg's comment:
Code
Output