6

Consider the following LaTeX manuscript, which uses the standalone document class to generate both a PDF as well as a PNG images of a TikZ picture.

\documentclass[tikz,convert]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) -- (1,1)
   [rounded corners] -- (2,0) -- (3,1)
   [sharp corners] -- (3,0) -- (2,1);
\end{tikzpicture}
\end{document}

The resulting PDF image is (this is a screenshot):

The resulting PDF image

The resulting PNG image is:

The resulting PNG image

Why does the PNG image have different, and wrong, dimensions than the PDF file?

  • 5
    It would probably be a good idea if you mentioned exactly how you made the PNG. And that MWE does not generated that image. (I can see this comes from the convert option, but others not familiar with this standalone option may not know this) – daleif Jul 4 '17 at 14:46
  • 5
    see the options to convert in the standalone manual, it assumes a density of 300dpi so it you look at the image at a smaller resolution it will appear larger. dimensions are probably not wrong they are just not what you expected. – David Carlisle Jul 4 '17 at 14:50
  • 2
    It seems unlikley that the posted code made the image shown, I just get ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. – David Carlisle Jul 4 '17 at 14:51
  • 1
    @DavidCarlisle have you tried it? I just added ` -- ++ (0,1);` and added [line width=2mm] to get something more noticable. Loading the PDF and PNG back into LaTeX gives a big difference (this is non scaled). – daleif Jul 4 '17 at 14:54
  • 1
    Also, exactly what is the size of the PDF? One is a vector graphic and other is a bitmap, so you'll probably never get an exact 1-1 size match. – daleif Jul 4 '17 at 14:56
24

Conceptually bitmaps do not have a size, they just have a number of pixels. As documented in the standalone manual the class tells convert to assume 300dpi which makes a high resolution image for print, but the default image resolution for pdftex is 72dpi, in fact png files can store an intended size for their pixels but most don't and even if they do not all systems use the hint.

so either of

  \fbox{\includegraphics[scale=.24]{file.png}}% 72/300=.24

or

 {\pdfimageresolution=300 \fbox{\includegraphics{file.png}}}

will be the same size as

 \fbox{\includegraphics{file.pdf}}
10

No. It does not. It depends how you measure it.

Measuring in MS Word:

Three different resolution images placed in Word:

enter image description here

When printed:

enter image description here

Measuring in LaTeX:

The Code:

    \documentclass[]{standalone}
    \usepackage{graphics}
    \begin{document}
    \noindent
    \includegraphics{aaa.pdf}
    \includegraphics{aaa.png}
    \end{document}

Measurement on the screen with 100% zoom of the PDF viewer:

enter image description here

Measurement on Web Page:

You cannot rely on the size of the PNG that you see on a web browser as they process images differently (see here)

JPEG Image with 1200 dpi Uploaded to Imgur and seen through a web browser:

enter image description here

PNG Image with 300 dpi Uploaded to Imgur and seen through a web browser:

enter image description here

JPEG Image with 50 dpi Uploaded to Imgur and seen through a web browser:

enter image description here

If you want to see the correct dimension on your web browser, you need to generate your PNG with the correct dpi. See the following exercise:

My screen is 2560x1440.

Horizontal dimension of screen is: appr 23.5 inches

Screen resolution is 2560 dots/23.5 inches = 109 dots per inch

I generated a PNG with a dpi of approximately 110.

I measure the horizontal dimension on the screen with a web browser:

enter image description here

  • 3
    sorry but I do not think that addresses the issue at all which is about png generated by imagemagic convert as called by standalone. – David Carlisle Jul 4 '17 at 15:46
  • 2
    "I think Imgur and StackExchage just zooms it while uploading it." that is essentially the whole point, a bitmap image has no natural size, your browser shows images at a default resolution (as does pdftex) so if you use images with different number of pixels they will appear at different sizes unless you specify a size. – David Carlisle Jul 4 '17 at 16:24
  • No sorry this is not a discussion forum, the answer should address the question at the top of the page, your posting gets longer and longer with pictures of rulers which don't seem to have any connection to the question. – David Carlisle Jul 4 '17 at 18:20
  • I am trying to push my students to learn more about LaTeX rather than use Word. This question is one reason for that, that is why I paid attention to it. I will refer it to my students later. Sorry for many edits. – berkus Jul 4 '17 at 20:13
  • Why would this encourage them to use LaTeX? If you want to write something for you students, that's fine, but why present it as an answer to a question which it does not address? – cfr Jul 4 '17 at 21:20
4

The standalone user manual (v1.2 - 2015/07/15) explains why the generated PNG image doesn't have the expected physical dimensions, and also gives methods to set these dimensions properly.

Why the PNG image has the wrong physical dimensions

According to section 4.6.1 'Conversion settings' on p. 15

the following default settings are used: [...] a density of 300dpi

'dpi' is short for 'dot per inch', a.k.a. 'pixels per inch' ('ppi'). To have the correct dimensions when displayed on your screen, the ppi used to generate the PNG must equal your screen's ppi.

To find out your screen's ppi, follow this simple method given on Wikipedia's Pixel density page. The method consists of using a ruler held to the screen to measure the length of a line known to be made up of a certain number of pixels.

How to ensure the PNG image has the correct physical dimensions

Table 1 on p. 17 suggests two methods to ensure the generated PNG image have the desired physical dimensions. Both methods consist of assigning a value to the convert document class option, as follows:

\documentclass[tikz,convert={...}]{standalone}
  1. Set convert={density=<d>}, where <d> is your screen's ppi.

  2. Set convert={size=<w>x<h>}, where <w>x<h> is the pixel dimensions of a PNG image known to have the desired physical dimensions when displayed on your screen.

    In the present case, since you stated that the screenshot of the PDF picture had the correct physical dimensions, use its pixel dimensions, namely 137x50. If you use macOS, these can be read directly from Finder.

  • it's not at all surprising that you get differences of a pixel or two in size (I'd have expected more) nowhere do you specify the image size so tikz has to estimate a bounding box, working out exactly the range of pixels made black by the bezier curves implied by the control points in the path is a tricky mathematical problem and almost all systems do not try to solve that exactly. a system like ghostscript can get accurate bounding box as it actually renders the image and can just look which pixels are non-white, but doing the same just from a specification of the path is tricky. – David Carlisle Jul 5 '17 at 18:58
  • I'm not sure what you mean by "On a given operating system all PNG files will have the same ppi's." as if I give you a png file generated on linux or windows and you save it on your mac, it does not change in any way or has any properties that distinguish it from png files generated on the mac. the ppi is just not a property of the png, it is a property of the software displaying it. – David Carlisle Jul 5 '17 at 19:01
  • @DavidCarlisle: I used an idiosyncratic definition of ppi (or rather ppi's, in plural: a horizontal ppi and a vertical ppi) that suited my theory of how things work. According to my definition, ppi is a ternary relation: the (horizontal/vertical) ppi of <a PNG file> in <a certain OS> with respect to <a screen>. What I tried to convey in the sentence you quoted was that this relation is in fact binary: given an OS and a screen, all PNG files will have the same ppi. In other words, a ppi is a property of an OS/screen combination. – Evan Aad Jul 5 '17 at 19:52
  • @DavidCarlisle: And, yes - moving a PNG file to another OS, or even just connecting a different screen to the same computer, may affect the ppi of the file (according to the definition I used in my answer). – Evan Aad Jul 5 '17 at 19:52
  • it is not a property of the screen or OS. if you include a png file into pdflatex it will use the resolution specified by pdflatex (72) and make the image the same size whatever OS you have, and if you include the image into a cross-OS browser such as chrome likewise it will be rendered at the resolution specified by chrome. – David Carlisle Jul 5 '17 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.