Subject: bug#20171: 11.88; Ghostscript resolution in preview-latex

From: Bob PEPIN <[email-masked]>
Date: Sun, 22 Mar 2015 23:08:52 +0100

Dear AucTeX developers,

I just started using AUCTeX on my Mac, and I noticed that the preview images all seemed the wrong size. After browsing a bit through the source code, it seems to me that the ghostscript resolution is calculated according to the following formula (l.nnn indicates line numbers in git 854140a32d7918b1013726244c546e762cd80773):

preview-gs-resolution 
 =(l.445) 1/mag * scale [pts/ptl] * xres [pts/in]
 =(l.1389, l.2986) 1/mag * (screen-font [pts] / latex-font [ptl]) * (screen-width [px] / screen-width [in])

units are in brackets, pts = screen points, ptl = latex points, in = inches, px = pixels
(screen-width is actually reported as pts on Mac, however the font sizes are not just off by a factor of 2)

Thinking about the problem for two minutes suggested the following solution:
We would like a letter in the screen font and in the bitmap produced by GS to be the same size in pixels.

The size in pixels of a screen letter is: 

  screen-font [px] = bsf [px/pts] * screen-font [pts] 

where bsf is what is called “backing scale factor” on MacOS, which is just pixels per screen point

The size in pixels of the output from GS is:

  latex-font [px] = latex-font [ptl] * 1/72 [in/ptl] * preview-gs-resolution [px/in]

Setting the two equal, we get

  preview-gs-resolution = 72 [in/ptl] * bsf [px/pts] * screen-font [pts] / latex-font [ptl] = 72 * bsf * scale

Comparing this to the first formula for preview-gs-resolution, it seems that the original assumption was that

  bsf = (screen-width [px] / screen-width [in]) / 72 = “physical" DPI [px/in] / 72

which at least on MacOS seems to no longer be the case.

Now, if all other UI toolkits report a physical DPI that leads to the correct bsf, it might be better to report this directly to Aquamacs, but I thought you guys might like to know about it anyway to check the situation with other toolkits.

And here is a preview-get-geometry function that produces correct previews on my Mac:

(defvar preview-backing-scale-factor 2.0
  "Screen pixels per screen point, 2.0 for Macs with Retina displays")

(defun preview-get-geometry ()
  "Transfer display geometry parameters from current display.
Returns list of scale, resolution and colors.  Calculation
is done in current buffer."
  (condition-case err
      (let* ((bsf preview-backing-scale-factor)
	     (prf (if (boundp 'preview-resolution-factor)
		      preview-resolution-factor
		    1.0))
	     (geometry
	      (list (preview-hook-enquiry preview-scale-function)
		    (cons (* prf bsf 72) (* prf bsf 72))
		    (preview-get-colors)))
	     (preview-min-spec
	      (* bsf
		 (preview-inherited-face-attribute
		  'preview-reference-face :height 'default)
		 0.1)))
	(setq preview-icon (preview-make-image 'preview-icon-specs)
	      preview-error-icon (preview-make-image
				  'preview-error-icon-specs)
	      preview-nonready-icon (preview-make-image
				     'preview-nonready-icon-specs))
	geometry)
    (error (error "Display geometry unavailable: %s"
		  (error-message-string err)))))

Best regards,
Bob

Emacs  : Aquamacs 3.2 GNU Emacs 24.4.51.2
(x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
 of 2014-11-07 (Aquamacs-3.2) on watson.local
Package: 11.88

Run buffer contents:

Running `Preview-LaTeX' on `CollectiveVariables' with ``xelatex  --synctex=1 "\nonstopmode\nofiles\PassOptionsToPackage{active,tightpage,auctex}{preview}\AtBeginDocument{\ifx\ifPreview\undefined\RequirePackage[displaymath,floats,graphics,textmath,sections,footnotes]{preview}[2004/11/05]\fi}" "\input" CollectiveVariables.tex''
This is XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.

No auxiliary output files.

(./CollectiveVariables.tex
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size11.clo))
(/usr/local/texlive/2013/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/local/texlive/2013/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2013/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/local/texlive/2013/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/local/texlive/2013/texmf-dist/tex/generic/ifxetex/ifxetex.sty))
(/usr/local/texlive/2013/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3names.sty
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3bootstrap.sty))
(/usr/local/texlive/2013/texmf-dist/tex/latex/etex-pkg/etex.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3basics.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3expan.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3tl.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3seq.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3int.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3quark.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3prg.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3clist.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3token.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3prop.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3msg.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3file.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3skip.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3keys.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3kernel/l3fp.sty)
(/usr/local/texlive/2013/texmf-dist...

[...]

	...sity $e^{-\beta V(x)}$, we can define
! Preview: Snippet 64 ended.(490372+0x650840).
<-><->
      
l.125 invariant measure of $X$
                               with density $e^{-\beta V(x)}$, we can define
[64]
! Preview: Snippet 65 started.
<-><->
      
l.125 invariant measure of $X$ with density $
                                             e^{-\beta V(x)}$, we can define
! Preview: Snippet 65 ended.(653634+0x2334745).
<-><->
      
l.125 ...ure of $X$ with density $e^{-\beta V(x)}$
                                                  , we can define
[65]
! Preview: Snippet 66 started.
<-><->
      
l.126 a new process $
                     Z_t$ on $\mathbb R^m$ with generator
! Preview: Snippet 66 ended.(490372+107642x723805).
<-><->
      
l.126 a new process $Z_t$
                          on $\mathbb R^m$ with generator
[66]
! Preview: Snippet 67 started.
<-><->
      
l.126 a new process $Z_t$ on $
                              \mathbb R^m$ with generator
! Preview: Snippet 67 ended.(494359+0x1043789).
<-><->
      
l.126 a new process $Z_t$ on $\mathbb R^m$
                                           with generator
[67]
! Preview: Snippet 68 started.
<-><->
      
l.127 \begin{align}
                   
! Preview: Snippet 68 ended.(5799986+0x27410673).
<-><->
      
l.139 \end{align}
                 
[68]
! Preview: Snippet 69 started.
<-><->
      
l.142 \begin{align}
                   
! Preview: Snippet 69 ended.(3210069+0x27410673).
<-><->
      
l.151 \end{align}
                 
[69]
! Preview: Snippet 70 started.
<-><->
      
l.153 \begin{align}
                   
! Preview: Snippet 70 ended.(921951+0x27410673).
<-><->
      
l.155 \end{align}
                 
[70]

LaTeX Warning: Empty bibliography on input line 157.

 )
(see the transcript file for additional information)
Output written on CollectiveVariables.pdf (70 pages).
SyncTeX written on CollectiveVariables.synctex.gz.
Transcript written on CollectiveVariables.log.

TeX Output exited as expected with code 1 at Sun Mar 22 21:17:12
Running `Preview-PDF2DSC' with ``pdf2dsc CollectiveVariables.pdf CollectiveVariables.prv/tmp264wlH/preview.dsc''

Preview-PDF2DSC finished at Sun Mar 22 21:17:12
Running `Preview-Ghostscript' with ``/usr/texbin/rungs -dOutputFile\=\(CollectiveVariables.prv/tmp264wlH/pr1-\%d.png\) -q -dDELAYSAFER -dNOPAUSE -DNOPLATFONTS -dPrinted -dTextAlphaBits\=1 -dGraphicsAlphaBits\=1 -sDEVICE\=png16m -r1289x1289''

Preview-Ghostscript finished at Sun Mar 22 21:17:16

current state:
==============
(setq
 AUCTeX-version "11.88"
 LaTeX-command-style '(("" "%(PDF)%(latex) %(extraopts) %S%(PDFout)"))
 image-types '(png gif tiff jpeg xpm xbm pbm)
 preview-image-type 'png
 preview-image-creators '((dvipng
			   (open preview-gs-open preview-dvipng-process-setup)
			   (place preview-gs-place) (close preview-dvipng-close))
			  (png (open preview-gs-open) (place preview-gs-place)
			   (close preview-gs-close))
			  (jpeg (open preview-gs-open) (place preview-gs-place)
			   (close preview-gs-close))
			  (pnm (open preview-gs-open) (place preview-gs-place)
			   (close preview-gs-close))
			  (tiff (open preview-gs-open) (place preview-gs-place)
			   (close preview-gs-close))
			  )
 preview-dvipng-image-type 'png
 preview-dvipng-command "dvipng -picky -noghostscript %d -o \"%m/prev%%03d.png\""
 preview-pdf2dsc-command "pdf2dsc %s.pdf %m/preview.dsc"
 preview-gs-command "/usr/texbin/rungs"
 preview-gs-options '("-q" "-dDELAYSAFER" "-dNOPAUSE" "-DNOPLATFONTS" "-dPrinted"
		      "-dTextAlphaBits=1" "-dGraphicsAlphaBits=1")
 preview-gs-image-type-alist '((png png "-sDEVICE=png16m")
			       (dvipng png "-sDEVICE=png16m")
			       (jpeg jpeg "-sDEVICE=jpeg")
			       (pnm pbm "-sDEVICE=pnmraw")
			       (tiff tiff "-sDEVICE=tiff12nc"))
 preview-fast-conversion t
 preview-prefer-TeX-bb nil
 preview-dvips-command "dvips -i -E %d -o %m/preview.000"
 preview-fast-dvips-command "dvips %d -o %m/preview.ps"
 preview-scale-function 'preview-scale-from-face
 preview-LaTeX-command '("%`%l \"\\nonstopmode\\nofiles\\PassOptionsToPackage{"
			 ("," . preview-required-option-list)
			 "}{preview}\\AtBeginDocument{\\ifx\\ifPreview\\undefined"
			 preview-default-preamble "\\fi}\"%' %t")
 preview-required-option-list '("active" "tightpage" "auctex"
				(preview-preserve-counters "counters"))
 preview-preserve-counters nil
 preview-default-option-list '("displaymath" "floats" "graphics" "textmath"
			       "sections" "footnotes")
 preview-default-preamble '("\\RequirePackage[" ("," . preview-default-option-list)
			    "]{preview}[2004/11/05]")
 preview-LaTeX-command-replacements nil
 preview-dump-replacements '(preview-LaTeX-command-replacements
			     ("\\`\\([^ ]+\\)\\(\\( +-\\([^ \\\\\"]\\|\\\\\\.\\|\"[^\"]*\"\\)*\\)*\\)\\(.*\\)\\'" "\\1 -ini -interaction=nonstopmode \"&\\1\" " preview-format-name ".ini \\5")
			     )
 preview-undump-replacements '(("\\`\\([^ ]+\\) .*? \"\\\\input\" \\(.*\\)\\'"
				"\\1 -interaction=nonstopmode \"&"
				preview-format-name "\" \\2")
			       )
 preview-auto-cache-preamble t
 preview-TeX-style-dir "/Applications/Aquamacs.app/Contents/Resources/lisp/aquamacs/edit-modes/auctex/latex"
 )

Output from running `/usr/texbin/rungs -h':
GPL Ghostscript 9.07 (2013-02-14)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
Usage: gs [switches] [file1.ps file2.ps ...]
Most frequently used switches: (you can use # in place of =)
 -dNOPAUSE           no pause after page   | -q       `quiet', fewer messages
 -g<width>x<height>  page size in pixels   | -r<res>  pixels/inch resolution
 -sDEVICE=<devname>  select device         | -dBATCH  exit after last file
 -sOutputFile=<file> select output file: - for stdout, |command for pipe,
                                         embed %d or %ld for page #
Input formats: PostScript PostScriptLevel1 PostScriptLevel2 PostScriptLevel3 PDF
Default output device: bbox
Available devices:
   alc1900 alc2000 alc4000 alc4100 alc8500 alc8600 alc9100 ap3250 appledmp
   atx23 atx24 atx38 bbox bit bitcmyk bitrgb bitrgbtags bj10e bj10v bj10vh
   bj200 bjc600 bjc800 bjc880j bjccmyk bjccolor bjcgray bjcmono bmp16 bmp16m
   bmp256 bmp32b bmpgray bmpmono bmpsep1 bmpsep8 ccr cdeskjet cdj1600 cdj500
   cdj550 cdj670 cdj850 cdj880 cdj890 cdj970 cdjcolor cdjmono cdnj500 cfax
   chp2200 cif cljet5 cljet5c cljet5pr coslw2p coslwxl cp50 cups declj250
   deskjet devicen dfaxhigh dfaxlow dj505j djet500 djet500c dl2100 dnj650c
   epl2050 epl2050p epl2120 epl2500 epl2750 epl5800 epl5900 epl6100 epl6200
   eplcolor eplmono eps9high eps9mid epson epsonc epswrite escp escpage
   faxg3 faxg32d faxg4 fmlbp fmpr fs600 gdi hl1240 hl1250 hl7x0 hpdj1120c
   hpdj310 hpdj320 hpdj340 hpdj400 hpdj500 hpdj500c hpdj510 hpdj520 hpdj540
   hpdj550c hpdj560c hpdj600 hpdj660c hpdj670c hpdj680c hpdj690c hpdj850c
   hpdj855c hpdj870c hpdj890c hpdjplus hpdjportable ibmpro ijs imagen
   inferno inkcov iwhi iwlo iwlq jetp3852 jj100 jpeg jpegcmyk jpeggray la50
   la70 la75 la75plus laserjet lbp310 lbp320 lbp8 lex2050 lex3200 lex5700
   lex7000 lips2p lips3 lips4 lips4v lj250 lj3100sw lj4dith lj4dithp lj5gray
   lj5mono ljet2p ljet3 ljet3d ljet4 ljet4d ljet4pjl ljetplus ln03 lp1800
   lp1900 lp2000 lp2200 lp2400 lp2500 lp2563 lp3000c lp7500 lp7700 lp7900
   lp8000 lp8000c lp8100 lp8200c lp8300c lp8300f lp8400f lp8500c lp8600
   lp8600f lp8700 lp8800c lp8900 lp9000b lp9000c lp9100 lp9200b lp9200c
   lp9300 lp9400 lp9500c lp9600 lp9600s lp9800c lps4500 lps6500 lq850
   lxm3200 lxm5700m m8510 mag16 mag256 md1xMono md2k md50Eco md50Mono md5k
   mgr4 mgr8 mgrgray2 mgrgray4 mgrgray8 mgrmono miff24 mj500c mj6000c
   mj700v2c mj8000c ml600 necp6 npdl nullpage oce9050 oki182 oki4w okiibm
   oprp opvp paintjet pam pamcmyk32 pamcmyk4 pbm pbmraw pcl3 pcx16 pcx24b
   pcx256 pcx2up pcxcmyk pcxgray pcxmono pdfwrite pgm pgmraw pgnm pgnmraw
   photoex picty180 pj pjetxl pjxl pjxl300 pkm pkmraw pksm pksmraw plan
   plan9bm planc plang plank planm png16 png16m png256 png48 pngalpha
   pnggray pngmono pnm pnmraw ppm ppmraw pr1000 pr1000_4 pr150 pr201
   ps2write psdcmyk psdrgb psgray psmono psrgb pswrite pxlcolor pxlmono
   r4081 rinkj rpdl samsunggdi sgirgb sj48 spotcmyk st800 stcolor sunhmono
   svg t4693d2 t4693d4 t4693d8 tek4696 tiff12nc tiff24nc tiff32nc tiff48nc
   tiff64nc tiffcrle tiffg3 tiffg32d tiffg4 tiffgray tifflzw tiffpack
   tiffscaled tiffsep tiffsep1 txtwrite uniprint xcf xes
Search path:
   %rom%Resource/Init/ : %rom%lib/ :
   /usr/local/share/ghostscript/9.07/Resource/Init :
   /usr/local/share/ghostscript/9.07/lib :
   /usr/local/share/ghostscript/9.07/Resource/Font :
   /usr/local/share/ghostscript/fonts :
   /usr/local/share/fonts/default/ghostscript :
   /usr/local/share/fonts/default/Type1 :
   /usr/local/share/fonts/default/TrueType : /usr/lib/DPS/outline/base :
   /usr/openwin/lib/X11/fonts/Type1 : /usr/openwin/lib/X11/fonts/TrueType :
   /usr/share/cups/fonts
Initialization files are compiled into the executable.
For more information, see /usr/local/share/ghostscript/9.07/doc/Use.htm.
Please report bugs to bugs.ghostscript.com.