7

The boxes and glue system seems to be both the strength and weakness of TeX. One of the major problems I often have to deal with is uneven distances between lines as in the example below.

enter image description here

Inserting some \smash commands around the math proves that this extra line spacing is not at all necessary; there is plenty of room for the math on lines of standard height:

enter image description here

Currently, inserting \smash commands manually is the only acceptable solution I have found to this problem, as I doubt messing with the interline glue is a good idea (or what?). I wonder if there is an automatic way to accomplish this? Can I prevent TeX from adjusting the line height based on the size of inline math?

You might think:

But what if you want to place large structures, like matrices, in inline math? Certainly you do not want them to collide with the adjacent lines?

But the fact is that I never place matrices or similar large constructions in inline math. In my opinion, such things belong entirely in displayed math.

\documentclass{article}

\usepackage{mathtools}

\DeclareMathOperator\Ad{Ad}

\usepackage{lipsum}

\begin{document}

Consider the map~$\Ad_{g_0^2}$.
La la la la la la la la la la la la la la la la la.
And the algebra~$\widetilde{A}$.
\lipsum[1]

Consider the map~\smash{$\Ad_{g_0^2}$}.
La la la la la la la la la la la la la la la la la.
And the algebra~\smash{$\widetilde{A}$}.
\lipsum[1]

\end{document}
lockstep
  • 250,273
Gaussler
  • 12,801
  • 1
    For small problems with \tilde{A}, you can use in the preamble something like \setstretch{1.06} from setspace. Personally, I consider the interline spacing in LaTeX is very tight. – Bernard Jul 16 '16 at 13:21
  • 2
    consider your example. you lucked out. it could have happened that the accented A came right under the expression with the subscript. then you be out of luck -- or you could rewrite. (la)tex does not know where on adjacent lines the potential conflicts appear; it's undoubtedly possible -- with an extensive redesign of the tex internals -- to record and use this information, but given the restrictions under which tex was created, it's quite amazing that it works as well as it does. nobody has yet improved the math handling, to the best of my knowledge. – barbara beeton Jul 16 '16 at 14:29
  • 1
    I am well aware that such collisions might happen. But they will happen independently of whether \smashing is done automatically or manually. In both cases, the solution for me is to rewrite the text. To be clear, I do not, under any circumstances, want uneven line spacing. I prefer TeX to let the lines collide instead and then leave it to me to rewrite what I find necessary. – Gaussler Jul 16 '16 at 14:36
  • if you use \(..\) for inline math then you just need \def\(#1\){\smash{$#1$} – David Carlisle Jul 16 '16 at 14:54
  • @DavidCarlisle Will there be unwanted side effects of doing this globally? – Gaussler Jul 16 '16 at 14:59
  • For instance, egreg discusses some here: http://tex.stackexchange.com/a/116484/19809 – Gaussler Jul 16 '16 at 15:02
  • Have you tried increasing \baselineskip? (Possibly \normalbaselineskip as well.) – John Kormylo Jul 16 '16 at 16:24

2 Answers2

4

Posting a second answer as I got drawn back to this page by a comment ping and I see that there is only a luatex answer when a simpler classic tex answer is possible.

If you set \lineskiplimit=-\maxdimen then tex will preserve baseline with no limit on the amount of overlapping that would cause.

enter image description here

\documentclass{article}

\usepackage{mathtools}

\DeclareMathOperator\Ad{Ad}

\usepackage{lipsum}

\begin{document}

\lineskiplimit=-\maxdimen

Consider the map~$\Ad_{g_0^2}$. La la la la la la la la la la la la la la la la la. And the algebra~$\widetilde{A}$. \lipsum[1]

Consider the map~\smash{$\Ad_{g_0^2}$}. La la la la la la la la la la la la la la la la la. And the algebra~\smash{$\widetilde{A}$}. \lipsum[1]

\end{document}

David Carlisle
  • 757,742
  • And if the high/low elements happen to be right above/below one another, they will be happy to overlap. (You've said that, though it doesn't stand out.) Safer might be \lineskiplimit=0pt; then adjacent anomalies will merely be set touching, not overlapping. – barbara beeton Jan 31 '21 at 00:21
  • @barbarabeeton the OP wants overlapping, preserving the baseline at all costs. – David Carlisle Jan 31 '21 at 00:23
  • 1
    "the OP wants overlapping" may be true, but some future newbie may be looking for a slightly less drastic solution without realizing it, and be rather surprised when there's a dense blob in the middle of a paragraph. – barbara beeton Jan 31 '21 at 00:31
  • well yes but the question is fairly explicit that it asks to stay on the grid, doing that and rewriting the text as needed is not a completely bad option. It is a shame the MWE from the question didn't have a few more la so that the accented A hit the subscript on the line above but so be it – David Carlisle Jan 31 '21 at 00:33
  • This solution breaks the spacing inside and around tikz-cd diagrams completely, for some reason. Can this be fixed somehow? – Gaussler Feb 09 '21 at 08:34
  • 1
    @Gaussler well presumably not broken but just as specified the lines overlap rather than space themselves off the grid, just put \lineskiplimit=\normallineskiplimit where you want to revert to the normal behaviour. – David Carlisle Feb 09 '21 at 08:46
  • This does not work for my case. Could you have a look? Thanks. https://gist.github.com/neruthes/9ace5a955a3d47f084b2b45625f932e4 – user3332315 Aug 01 '22 at 09:10
  • @user3332315 look at what? Make an example and post a question if you have a question about alignment – David Carlisle Aug 01 '22 at 09:12
  • @DavidCarlisle Here is the question, thanks. https://tex.stackexchange.com/questions/652624 – user3332315 Aug 01 '22 at 09:18
3

Here's a luatex version that could be made more robust but after the linebreaking it uses a callback to set the height and depth of boxes and (s) replace any baselineskip or lineskip glue by a fixed value. (Probably it should preserve the height of the first box and the depth of the last, left as an exercise..

enter image description here

\documentclass{article}

\usepackage{mathtools}

\DeclareMathOperator\Ad{Ad}

\directlua{ function fixlines(h,c) local pd=0 print('code: ' .. type(c) .. ':' .. c) for n in node.traverse(h) do % % smash boxes if n.id==0 then n.depth=0 n.height=0 end % % lineskip or baselineskip set to 12pt if n.id==12 and (n.subtype==1 or n.subtype==2) then n.width=786432 % 12pt in sp end end return h end luatexbase.add_to_callback('post_linebreak_filter', fixlines, 'fix line spacing') } \usepackage{lipsum}

\begin{document}

Consider the map~$\Ad_{g_0^2}$. La la la la la la la la la la la la la la la la la. And the algebra~$\widetilde{A}$. \lipsum[1]

Consider the map~\smash{$\Ad_{g_0^2}$}. La la la la la la la la la la la la la la la la la. And the algebra~\smash{$\widetilde{A}$}. \lipsum[1]

\end{document}

muzimuzhi Z
  • 26,474
David Carlisle
  • 757,742
  • What do you mean with making more robust? Can you tell me an idea? :) – Weißer Kater Jan 30 '21 at 22:55
  • @WeißerKater your guess of what was in my head 4 years ago is as good as mine:-) I would think apart from the first and last line issues I mention i was concerned that I am not checking if there is any existing use of this callback, but looking at this I think it is over-complicated and I posted another answer just now. – David Carlisle Jan 31 '21 at 00:04
  • I like this answer. It gave me some ideas to get grid typesetting, see https://tex.stackexchange.com/questions/581293/using-post-linebreak-filter-in-luatex-to-set-height-and-depth-of-boxes. :) – Weißer Kater Jan 31 '21 at 14:24