skip to Main Content

I’m trying to add the capability to render LaTeX equations to a project I’m working on. To do so, I use XeLaTeX to create a PDF file, which I then render to a (transparent) 96dpi-PNG using Ghostscript.

I’d like to have the rendered LaTeX blend in with the rest of the text (which is rendered using standard .NET GDI+ methods, but that’s off-topic), but I can’t get a reliably “good” text rendering: the output always looks somehow blurry or otherwise “bad”.

Example:

Comparison

From left to right, the same (small) PDF rendered at 96dpi with Ghostscript, Photoshop, and TexWorks (which I understand uses Ghostscript internally).

The command I use to run Ghostscript is the following:

"C:/Program Files (x86)/gs/gs9.09/bin/gswin32c.exe" 
      -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT 
      -dMaxBitmap=500000000 -dAlignToPixels=1 -dGridFitTT=2 
       "-sDEVICE=pngalpha" -dTextAlphaBits=4 
      -dGraphicsAlphaBits=4 "-r96" -dFirstPage=1 -dLastPage=1 
      -sOutputFile="output.png" "input.pdf"

(which I actually pretty much copied from the command ImageMagick calls when converting a PDF file, but that’s another story). I tried changing any of the relevant options (dAlignToPixels=0, dGridFitTT=0/1/2, dTextAlphaBits=2/4 [or without this parameter altogether]) and I even tried to render the PDF to 4 times the resolution and then downscale it, without any noticeable improvement.

Yet, I’m sure there must be some way of decently rendering the PDF with Ghostscript (since TexWorks does), although I’m unable to find it.

Any hint? The PDF is this one.

2

Answers


  1. You are rendering text at 11 points, at 96 dpi, that works out to about 14 pixels in height which, frankly, is not a lot (and in my output the ‘s’ is 7 pixels high by 4 wide). Looking at your output all 3 look ‘blurry’ and the Photoshop output looks overly bold in the capital T.

    If you don’t want it blurred, then don’t set TextAlphaBits, or don’t set it to such a high value.

    I’d also suggest using the current release (9.15).

    Login or Signup to reply.
  2. You could try to render your PDF at a higher resolution. 96dpi just isn’t enough for text with 11 pt size.

    If you use 192dpi and then scale the display of the resulting image to 50% (wherever you use the PNG), these parts should still appear in the same size as befor, but with a higher resolution. What used to be a 4×7 pixels ‘s’ should now be a 8×14 pixels ‘s’


    Update

    Ok, since my explanation seems to have been not comprehendible enough for the OP, here’s the deal.

    1. Generate a PDF file containing the word “Test”, using Ghostscript. In my case, it is Ghostscript v9.10:

      gs                       
       -o test.pdf             
       -sDEVICE=pdfwrite       
       -g230x100               
       -c "/Helvetica findfont 
           11 scalefont        
           setfont             
           1 1 moveto          
           (Test) show         
           showpage"
      
    2. From this PDF, generate 6 different images depicting the word “Test”, using 6 different resolutions. The gs is still Ghostscript v9.10 (to be checked with gs -version):

      for i in 1 2 3 4 5 6; do      
          gs                        
           -o t$(( ${i} * 96 )).png 
           -r$(( ${i} * 96 ))       
           -sDEVICE=pngalpha        
           -dAlignToPixels=1        
           -dGridFitTT=2            
           -dTextAlphaBits=4        
           -dGraphicsAlphaBits=4    
            t.pdf ;                 
      done
      

      This will create the following PNGs, as confirmed by ImageMagick’s identify command:

      identify -format "%f :   %Wx%H pixels  --  %b filesizen" t[1-9]*.png
        t96.png :   31x13 pixels  --     475B filesize
       t192.png :   61x27 pixels  --     774B filesize
       t288.png :   92x40 pixels  --    1.1KB filesize
       t384.png :  123x53 pixels  --   1.43KB filesize
       t480.png :  153x67 pixels  --   1.76KB filesize
       t576.png :  184x80 pixels  --   2.01KB filesize
      
    3. Create a sample LaTeX document and embed the different images side by side and/or line by line. Here is my sample code:

      begin{document}
      Test
      includegraphics[height=7.5pt]{t96.png}
      includegraphics[height=7.5pt]{t96.png}
      includegraphics[height=7.5pt]{t192.png}
      includegraphics[height=7.5pt]{t288.png}
      includegraphics[height=7.5pt]{t384.png}
      includegraphics[height=7.5pt]{t480.png}
      includegraphics[height=7.5pt]{t576.png}
      Test\
      
      {}
      
      Test <== real text
      
      includegraphics[height=7.5pt]{t96.png}  <-- 96 dpi figure
      
      includegraphics[height=7.5pt]{t192.png} <-- 192 dpi figure
      
      includegraphics[height=7.5pt]{t288.png} <-- 288 dpi figure
      
      includegraphics[height=7.5pt]{t384.png} <-- 384 dpi figure
      
      includegraphics[height=7.5pt]{t480.png} <-- 480 dpi figure
      
      includegraphics[height=7.5pt]{t576.png} <-- 576 dpi figure
      
      Test <== real text
      end{document}
      
    4. Here is a screenshot (at 400% zoom) from the PDF created via LuaLaTeX from the above LaTeX code:

      Screenshot from resulting PDF page

      The line with the 8 “Test” words has actual text only in the first and the last word. The 6 words in between are images with 96, 96, 192, 288, 384, 480 and 576 dpi.

    I hope you can see now clearly how scaling up your image generation to a higher resolution will result in better quality for your final PDF if you include the higher resolution images into your LaTeX code…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search