63

It has been pointed out on this site, e.g. here by egreg, that text punctuation doesn't belong to the formula in inline math. I've been following this practice for a long time, so I write for all $v\in V$, where ... as opposed to for all $v\in V,$ where .... However, the first version prevents kerning between V and the comma. Note that the comma is a lot closer to the V in the second version:

output with V

\documentclass{article}
\begin{document}
$v\in V$, where \quad $v\in V,$ where 
\end{document}

Note that there's no difference between the two versions if I replace V with A:

output with A

Now I'm wondering: Should I start including punctuation in my formulas to enable proper kerning, at least as long as fonts are used where the text and math punctuation glyphs look the same? Or are there reasons why the kerning is not desirable anyway?

  • 7
    Good question (not sure if it has a good answer though) – David Carlisle Mar 5 '13 at 20:28
  • A slightly related discussion was on chat some time ago if a word starts with capital A and then a vertical character follows. It certainly looks odd but I guess one can live with that. chat.stackexchange.com/transcript/message/7786643#7786643 – percusse Mar 5 '13 at 20:29
  • To preserve the font of the , while fixing the distance, also $v\in V\!$, could be used here, while other combination of a letter and some punctuation might require a different negative horizontal space. – Stephen Mar 5 '13 at 20:39
  • @percusse: There it seems to be a decision (or slackness?) of the font designer, i.e. DEK: the A bsence could be mollified in the kerning table :-) – Hendrik Vogt Mar 5 '13 at 20:48
  • 1
    @deimi: Good observation! This is laid down in Appendix G of the TeXbook. Kerning in math mode only happens after very simple Ord atoms, not after accented characters. So it's by design, but I'd say that I don't really like it! – Hendrik Vogt Mar 6 '13 at 10:09
24

If you want to maintain the kern but still keep your math expressions logically pure of sentence punctuation, and practically available for use in a document class that uses different text and math fonts, you could add a hand correction after the math, but in practice I'm not sure you'd want to do this:

\documentclass{article}
\def\kn#1#2{{\sbox0{$#1#2$}\sbox2{$#1{}#2$}\kern\dimexpr\wd0-\wd2\relax}#2}
\begin{document}
$v\in V$\kn V, where \quad $v\in V,$ where 
\end{document}

enter image description here

  • Thanks for the workaround. But indeed, this impairs the readability of the code so much that I'd use it only if the text and math commas differ too much. Would you personally prefer to maintain the kern if there was an easy solution that is "logically pure"? – Hendrik Vogt Mar 5 '13 at 20:53
  • I can't believe I'd do that if writing the code by hand (I'd probably put the comma outside and just not worry) If I was generating the tex from something else, I might do it though... – David Carlisle Mar 5 '13 at 21:28
  • Sorry, I don't quite get what you mean. Maybe my previous comment was unclear. What I meant is this: would you deem it good if just typing $v\in V$, where would yield the tighter kerning? – Hendrik Vogt Mar 5 '13 at 21:32
  • 2
    yes I think it looks better but since TeX doesn't do that I probably wouldn't risk breaking everything trying to insert some automatic xspace style lookahead with \everymath. I'd either just live without the kern or genarate the tex with the correction applied where necessary. Not so much worse than having to add \/ sometimes/ – David Carlisle Mar 5 '13 at 21:51
9

(updated the code to ensure that the Lua function doesn't perform string substitutions inside verbatim-like environments)

Here's a LuaLaTeX-based approach. It combines the \kn macro from David Carlisle's earlier answer with a Lua function (assigned to the process_input_buffer callback) that scans the input stream and automatically replaces all matches of <Upppercase Letter>$<punctuation mark> with David C's code. The Lua function may be switched on and off via the LaTeX macros \adjustcommaOn and \adjustcommaOff.

The application used in the following code is inspired by this question, which was closed as a duplicate of the present question. Note that the Lua function suspends its operations if LaTeX is inside verbatim-like environments such as verbatim, Verbatim, and lstlisting.

enter image description here

\documentclass{article}
% cf. https://tex.stackexchange.com/a/101059/5001:
\def\kn#1#2{{\sbox0{$#1#2$}\sbox2{$#1\mkern0mu#2$}\kern\dimexpr\wd0-\wd2\relax}#2}

\usepackage{luacode,geometry}
\begin{luacode}
in_verbatim = false
function ajust_punctuation ( s )
   if string.find     ( s , "\\begin{[vV]erbatim}" ) 
      or  string.find ( s , "\\begin{lstlisting}" ) then
          in_verbatim = true
   elseif string.find ( s , "\\end{[vV]erbatim}" ) 
      or  string.find ( s , "\\end{lstlisting}" ) then
          in_verbatim = false
   elseif in_verbatim == false then
      -- scan for "<UppercaseLetter><$><PunctuationMark>" pattern:
      s = string.gsub ( s , "(%u)%$(%p)", "%1$\\kn %1%2" ) 
   end
   return s 
end
\end{luacode}
\newcommand{\adjustcommaOn}{%
    \luadirect{luatexbase.add_to_callback(
   "process_input_buffer", ajust_punctuation, "ajust_punctuation" )}}
\newcommand{\adjustcommaOff}{%
   \luadirect{luatexbase.remove_from_callback(
   "process_input_buffer", "ajust_punctuation" )}}

\setlength\parindent{0pt} % just for this example
\obeylines 

\begin{document}

Proper spacing: $A$, $E$, $Q$, $R$. 
\adjustcommaOn
Proper spacing: $A$, $E$, $Q$, $R$. --- Virtually no change.

\smallskip
\adjustcommaOff
Acceptable spacing: $B$, $C$, $D$, $G$, $O$, $S$, $Z$. 
\adjustcommaOn
Acceptable spacing: $B$, $C$, $D$, $G$, $O$, $S$, $Z$. --- A little bit better.

\smallskip
\adjustcommaOff
Poor spacing: $F$, $H$, $I$, $J$, $K$, $M$, $N$, $P$, $T$, $U$, $V$, $W$, $X$, $Y$.
\adjustcommaOn
Poor spacing: $F$, $H$, $I$, $J$, $K$, $M$, $N$, $P$, $T$, $U$, $V$, $W$, $X$, $Y$. --- A major improvement.

% Note that "\adjustcommaOn" is in effect.
\begin{verbatim} 
Proper spacing: $A$, $E$, $Q$, $R$.
Acceptable spacing: $B$, $C$, $D$, $G$, $O$, $S$, $Z$.
Poor spacing: $F$, $H$, $I$, $J$, $K$, $M$, $N$, $P$, $T$, $U$, $V$, $W$, $X$, $Y$.
\end{verbatim}
\end{document}
  • Thank you for this nice solution for inline math. Is there also a way to extend this to display mode, e.g. \[ F\text. \] (but all other environments as well)? Possibly the \text command can be modified to automatically insert \kn if a punctuation is the first character. – Benjamin Desef Sep 15 '16 at 8:15
  • I just came up with a problem if this is globally activated. Assuming an axis label in a pgfplots, you will most likely find \begin{axis}[xlabel=$A$, ylabel=$B$, ...], which will be replaced as well and thus not compile. I don't know Lua patterns good enough - is there a way to prevent this replacement by including the inline math in braces? – Benjamin Desef Sep 15 '16 at 9:40
  • @BenjaminDesef - On your first comment: It is my impression that people -- including experienced typographers -- often wish to increase, not decrease, the amount of space that's inserted immediately before the punctuation mark that terminates a display-math structure. – Mico Sep 15 '16 at 19:02
  • @BenjaminDesef - On your second comment: I've updated the Lua code so that no string substitutions are performed inside verbatim-like environments such as verbatim, Verbatim (provided by the fancyvrb package), and lstlisting (provided by the listings package). Nevertheless, it may not be a good idea to execute \adjustcommaOn globally, i.e., for the entire document, especially if other environments -- such as the axis environment you've identified -- also may use of patterns such as "$A$,". – Mico Sep 16 '16 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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