20

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?

  • 1
    Convert to .png yourself? – Joseph Wright Apr 19 '12 at 18:26
  • 1
    @JosephWright: Andrew provided a nice solution in the chat. So I asked this question ;-) – Marco Daniel Apr 19 '12 at 18:27
23

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 ;-)

  • Your class becomes better and better ;-) – Marco Daniel Apr 19 '12 at 18:34
  • 1
    It'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. – Loop Space Apr 19 '12 at 18:49
12

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)

7

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.

  1. We sample at density 96 * 4 = 384 dpi;
    • This creates a four-times-as-large PNG.
  2. 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

4x4 oversampling

Example of 1->1 sampling

convert -density $((96 * 1)) mwe-khatt.pdf mwe-khatt-per1.png

enter image description here

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!) – Esteis Apr 27 '13 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). – Speravir Apr 27 '13 at 20:49
  • 1
    TeX 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 the s, – the stroke is distorted or even broken; or, in the case of bridge of the h of the last 'the', there is a bit of a wart on the underside. Note also the distorted x-height of the n in the first 'ibn'. Computer/Latin Modern, with its many thin strokes, gets mangled even worse if you don't anti-alias. – Esteis Apr 27 '13 at 21:45
  • 1
    So my impression is right (hint: I intentionally used “As information” ;-) ). – Speravir Apr 27 '13 at 22:03
  • 1
    Not critical, but I believe 96 * 4 = 384 dpi:) Also, a great and illustrative answer! – andselisk Jul 28 at 11:54
  • 1
    Thanks, @andselisk, for your kind words and sharp eyes. I've corrected the error. :-) – Esteis Jul 30 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .