TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with a proprietary software (Matlab) that internally uses a LaTeX interpreter and then renders the resulting dvi-file. The problem is that the rendering part does not use the proper font metrics. The rendering contains the correct symbols, but they are not in the correct places because they have been positioned according to the wrong metrics. I can influence the part that produces the dvi, but not the rendering part.

My idea is to circumvent this problem by having a dvi that explicitly positions every single character. I can achieve this approximately by using weird LaTeX code. E.g.

$E = m c^2$

leads to the wrong display, basically piling all the characters in the same position (font width metrics used by the renderer are far too small). Using instead

$\mbox{E} \mbox{=} \mbox{m} \mbox{c}^2$

leads to roughly the right display, presumably because the resulting dvi explicitly codes the width of each mbox, and thereby the width of each symbol. Another example:

$\hat D_s$

renders the D with hat correctly, but the subscript s is shown on top of the D instead of after it. I can fix this again using the same trick:


Btw., the problem does not just apply to math mode, its just what I have been concretely working on.

My question: Is there a way to make (La)TeX produce a dvi-file that explicitly positions each symbol in a way that I have approximated by inserting \mboxes? Or is there a way to post-process the dvi-file into another dvi-file that contains such explicit positioning information?

share|improve this question

The documentation of dvitype contains the description of the DVI format in section "Device-independent file format". dvitype can also be used to analyze the DVI file.


  $E = m c^2$

"mc" are set the following way (excerpt from dvitype):

169: fntnum10 current font is cmmi10 
170: setchar109 h:=6441634+575415=7017049, hh:=444 
171: setchar99 h:=7017049+283611=7300660, hh:=462 

The DVI processor sets m (char code 109), looks up the font metrics in cmmi10 to get the width of the character and outputs the next character c (char code 99).

\mbox{$m$}\mbox{$c$} creates:

110: push 
level 3:(h=0,v=4128768,w=0,x=0,y=0,z=0,hh=0,vv=262) 
111: right3 5046272 h:=0+5046272=5046272, hh:=320 
[ ]
115: fntdef1 10: cmmi10 
137: fntnum10 current font is cmmi10 
138: setchar109 h:=5046272+575415=5621687, hh:=356 
139: pop 
level 3:(h=0,v=4128768,w=0,x=0,y=0,z=0,hh=0,vv=262) 
140: push 
level 3:(h=0,v=4128768,w=0,x=0,y=0,z=0,hh=0,vv=262) 
141: right3 5621687 h:=0+5621687=5621687, hh:=356 
145: setchar99 h:=5621687+283611=5905298, hh:=374 
[ c]
146: pop 

Now the position before the character is saved (push), the character is positioned using right3, and the old position is restored pop. This way, there is no need to consult the font metrics to get the widths of the characters.

If you want to solve the issue at the DVI stage, write a post-processor that reads and parses the DVI file, calculates the correct positions of the characters with the help of the font metrics and rewrites the file using the positioning method without the need of font metrics, see above.

On the other hand, Matlab is AFAIK a commercial software. If you have bought it, complain and make a bug report.

share|improve this answer
Thanks, good to know, but I doubt I'll get around to actually do this myself. I had hoped there would be some easy trick or available tool to do this. As for the bug report, I did report this and others did, too, but that doesn't mean it is going to get fixed anytime soon; they have many customers, and I guess most of them are not that much into math typesetting. – In the meantime I decided to make my proper graphs not using Matlab's functions but I wrote a little Matlab library that generates TikZ code and renders that to pdf, resulting in much better quality, not just regarding typesetting. – A. Donda Apr 9 '14 at 0:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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