Very often I use the document class standalone
to created a cropped PDF for uploading to tex.stackexchange. However the resolution after the upload isn't very good. What can I do?
3 Answers
The PDF to PNG conversion of imgur isn't very good and results in a poor resolution. You should convert it yourself to a PNG (no JPGs please!).
Note that the standalone
class (v1.0) supports conversion to PNG. It works well with Image Magick under Linux, but isn't really tested under Windows by me.
It needs the -shell-escape
compiler switch to call convert
internally.
You can enable conversion using the convert
class options.
There are settings for the size and density as well.
E.g. use:
\documentclass[convert={png,size=630,density=300}]{standalone}
to create a 630px wide PNG with 300dpi. Note that 630px is the maximal width for images in stackexchange posts. Beyond that, they will be zoomed-down. Put the density
higher for smaller content. The size
will resize the image, so the final density might be different.
I actually planned a special TeX.SX / Stackexchange mode, which sets suitable parameters automatically, but never implemented it.
If you have issues making this work (especially under untested OSs) than run Image Magick convert
manually:
convert -density <density> <FILE>.pdf -resize <SIZEX>x<SIZEY> -quality 90 <FILE>.png
The quality
setting here is just used for the PNG compression and has no other effect on this loss-less format. The x<SIZEY>
is optional.
Note that under MS Windows there already is a convert
executable (by Microsoft) which is for converting FAT32 hard drives to NTFS. So either the full path to Image Magick must be given or its convert
should be renamed to imgconvert
. Actually under Windows standalone
will use imgconvert
to not accidentally convert your hard drives ;-)
-
-
1It's worth checking if the zooming down is only when the image is displayed in a post or is a real zooming down. If not real then it can be worth uploading a higher resolution as clicking on the image loads it in a new window at full resolution. Commented Apr 19, 2012 at 18:49
There can be reasons why it is useful to have a command-line method to convert PDFs to PNGs instead of letting TeX do everything - for example, if producing the PDF takes a long time and you don't know what resolution you want, starting again from the PDF is easier than starting again from the original TeX file. Anyway, for those occasions the netpbm
library is the Swiss Army Knife of command-line image manipulation for Linux/Unix. For converting a PDF to a PNG, the command is:
pdftoppm -scale-to 500 letter-shapes-test-crop.pdf|pnmtopng > madman.png
(bonus points for guessing which answer this was used for)
-
1
-
Two clarifications:
pdftoppm
comes withpoppler-utils
whilepnmtopng
comes withnetpbm
. Commented Dec 31, 2012 at 1:51
I suspect the poor resolution you are talking about might be poor antialiasing. This is the convert
one-liner you want:
convert -density 384 mwe.pdf -resize 25% mwe.png
Explanation
Standard monitor resolution is 96 dots per inch, so simply sampling the pdf at 96 dpi produces a PNG image equal in size to PDF image on your screen.
# this looks ugly
convert -density 96 mwe.pdf mwe.png
Unfortunately, if you do this you'll get horribly jagged edges on your letter. Because each pixel is only one sample, a pixel that falls half on a letter and half off it will either be either black (an ugly bump on the letter) or white (an ugly bite). Goodbye, smooth edges. The solution is anti-aliasing, by oversampling the image and then shrinking it.
- We sample at density 96 * 4 = 384 dpi;
- This creates a four-times-as-large PNG.
- We scale this PNG down 25 %;
- Now the value of each pixel is the average of 16 subsamples, instead of just one. As a result, the edges of letters look smooth instead of pixelated.
Example of (4x4)->1 oversampling
convert -density $((96 * 4)) mwe-khatt.pdf -resize 25% mwe-khatt-per4.png
Example of 1->1 sampling
convert -density $((96 * 1)) mwe-khatt.pdf mwe-khatt-per1.png
Source code of the example
\setupbodyfont[schola, 12pt]
\startTEXpage[width=10cm, offset=3mm]
\section{\CONTEXT}
\input khatt-en
\stopTEXpage
-
Example images added. And you're right, dpi is 96 — 72 is the number of Adobe points per inch. (Evil incheses! Hates them, we does!)– EsteisCommented Apr 27, 2013 at 20:41
-
OK, thanks. As information: Here, in first example image the font appears slightly, but noticeable bolder (“here” means a Windows system with 96 dpi and TFT screen).– SperavirCommented Apr 27, 2013 at 20:49
-
1TeX Gyre Schola is quite a bold font, so the upper image is more correct. In the lower image: notice that where the letters are thin – like the bridge of the
r
, and the vertical strokes of thes
, – the stroke is distorted or even broken; or, in the case of bridge of theh
of the last 'the', there is a bit of a wart on the underside. Note also the distorted x-height of then
in the first 'ibn'. Computer/Latin Modern, with its many thin strokes, gets mangled even worse if you don't anti-alias.– EsteisCommented Apr 27, 2013 at 21:45 -
1So my impression is right (hint: I intentionally used “As information” ;-) ).– SperavirCommented Apr 27, 2013 at 22:03
-
1Not critical, but I believe 96 * 4 = 384 dpi:) Also, a great and illustrative answer! Commented Jul 28, 2019 at 11:54
-
1Thanks, @andselisk, for your kind words and sharp eyes. I've corrected the error. :-)– EsteisCommented Jul 30, 2019 at 21:08
.png
yourself?