5

There is insufficient spacing between the lines in the MWE, made using an \hbox filled with two \vtop boxes.

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -1\colsep

\def\twocols#1#2{%                                                                                                                          
    \hbox to \linewidth{%                                                                                                                   
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #1\par}\hfill
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #2\par}\hss
    }\par
}

\twocols
  {This is a very long line that will run over}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

\bye

enter image description here

musarithmia
  • 12,463
  • 1
    Related: http://tex.stackexchange.com/questions/34971/how-to-keep-a-constant-baselineskip-when-using-minipages-or-parboxes – egreg Feb 21 '16 at 23:52

4 Answers4

6

You can carry over the depth of the last line in the \vtop:

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -1\colsep

\def\twocols#1#2{%
  \par
  \setbox0=\vtop{\hsize=\colwidth
    \hangindent=1em \hangafter=1 #1\par
    \xdef\tpdA{\the\prevdepth}
  }%
  \setbox2=\vtop{\hsize=\colwidth
    \hangindent=1em \hangafter=1 #2\par
    \xdef\tpdB{\the\prevdepth}
  }%
  \edef\tpd{\ifdim\dp0>\dp2 \tpdA \else \tpdB\fi}%
  \hbox to \linewidth{\box0 \hfill \box2}
  \prevdepth=\tpd
}

\twocols
  {This is a very long line that will run over over over over over over over}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

\bye

enter image description here

egreg
  • 1,121,712
3

If you turn on logging you see the dreaded

..\glue(\lineskip) 1.0

a vtop box has large depth, and the following vtop tries to maintain \baselineskip spacing to the reference point of the previous box which is the baseline of the first line. As that can not be achieved, TeX gives up on baseline spacing and just inserts \lineskip glue.

You can use a strut at the start and the end, and suppress lineskip glue then spacing will be preserved as long as there is no abnormally large text

enter image description here

\tracingonline1
\tracingoutput1

\showboxdepth=4

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -1\colsep

\def\twocols#1#2{%
    \hbox to \linewidth{% 
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 \strut#1\nobreak\strut\par}\hfill
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 \strut#2\nobreak\strut\par}\hss
    }\par
}

\twocols
  {This is a very long line that will run over}
  {What is wrong with the spacing?}
\nointerlineskip
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

\bye
David Carlisle
  • 757,742
1

Update handles the lineskip. Don't use this too close to bottom of page.

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -1\colsep

\def\twocols#1#2{%
    \par
    \hbox to\linewidth
    {%
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #1\par\indent}\hfill
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #2\par\indent}%
    }\kern-\baselineskip\hrule height0pt
    \indent\par
}

\lineskip\maxdimen

\twocols
  {This is a very long line that will run over over over over over over over ppppp}
  {Nothing wrong with
  the spacing !}
\twocols
  {Nothing \smash{\vrule height \baselineskip depth 0pt  width 1pt} wrong with
  the spacing! YYYYYY ppppppp}
  {This is a very long line that will run over}

AAAAAA\smash{\vrule height \baselineskip depth 0pt  width 1pt}

\showboxbreadth \maxdimen \showboxdepth \maxdimen

\tracingoutput1
\bye

enter image description here


Here is a a third approach. Does not use \prevdepth neither \strut's (the latter forces its use also if you follow the thing with something else than \twocols itself).

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -1\colsep

\def\twocols#1#2{%
    \par
    \hbox to\linewidth
    {%
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #1\par\indent}\hfill
      \vtop{\hsize=\colwidth
        \hangindent=1em \hangafter=1 #2\par\indent}%
    }\par\kern-\baselineskip\indent\par
}

\twocols
  {This is a very long line that will run over over over over over over jjjjj over}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

\bye

Blockquote

  • 2
    you have an additional 1pt (\lineskip) between the boxes still: ..\kern -12.0 ..\glue(\parskip) 0.0 plus 1.0 ..\glue(\lineskip) 1.0 – David Carlisle Feb 22 '16 at 09:25
  • 1
    The same as for Gustavo applies here: try $\Bigg|$ at the end of the very first box. – egreg Feb 22 '16 at 10:11
  • @egreg there is an approval vote of your comment, but the $\Bigg|$ elicits no surprise for me, to the contrary, this is was it aimed at here. –  Feb 22 '16 at 11:11
  • @GustavoMezzetti yes, oversight from copying David's answer as starting point ;-) although I had removed the unneeded \hss... ;-) –  Feb 22 '16 at 11:14
  • @jfbu The result is clearly unacceptable; is this what you were trying to say? So the answer serves no purpose, doesn't it? – egreg Feb 22 '16 at 11:14
  • @egreg I don't know what is the purpose of the OP. I thus interpreted it as maintain baselineskip separation and advanced an amusing approach which has even more defects than what appears to you as being one (I am surprised you didn't ask what happens at bottom of page). There is the lineskip glue I didn't check for as pointed out by David. I had upvoted your answer earlier posting mine as it was the obvious right thing. But this does not mean one can not try other fun approaches. –  Feb 22 '16 at 11:22
  • @egreg "maintain baselineskip separation" under normal circumstances. I make this more precise because I don't want that people think I pretended the code would maintain it in all circumstances. With objects of big depth, the result is not acceptable, I agree on that. –  Feb 22 '16 at 11:37
  • @DavidCarlisle I should have checked. See update. –  Feb 22 '16 at 12:04
  • I removed my comment about the superfluous \par: suggest you do the same with your reply. (This comment will be deleted as well.) – GuM Feb 22 '16 at 12:29
  • Instead of \hrule height 0pt you could just say \nointerlineskip: it’s the same. I think that yours is an interesting idea, and that it is always good to show different approaches: readers might find this instructive. I also appreciate your point about the \parskip glue. +1 :-) – GuM Feb 22 '16 at 14:00
1

Well, why not just the following, then?

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -\colsep

\def\singlebox#1{%
  \vtop{%
    \hsize=\colwidth
    \hangindent=1em \hangafter=1
    \leavevmode #1\par
    \kern -\prevdepth
  }%
}
\def\twocols#1#2{%
    \par
    \hbox to\linewidth
    {%
      \singlebox{#1}\hfill\singlebox{#2}%
    }%
    \prevdepth = 0pt
}

\twocols
  {This is a very long line that will run over over over over over over jjjjj over}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

More text.

% {\tracingall\showlists}

\bye

Quite obviously, since it “kills” the depth of the bottom line inside the \twocols construction, this solution is not adequate when the text contains unusual descenders (as egreg has immediately noticed). This answer was intended just as a streamlining of jfbu’s idea.

A possible refinement is:

\parindent=0pt

\newdimen\linewidth
\linewidth=4in

\newdimen\colsep
\colsep=1em

\newdimen\colwidth
\colwidth=0.5\linewidth
\advance\colwidth by -\colsep

\def\singlebox#1{%
  \vtop{%
    \hsize=\colwidth
    \hangindent=1em \hangafter=1
    \leavevmode #1\par
    \kern -%
      \ifdim\prevdepth<\maxdepth
        \prevdepth
      \else
        \maxdepth
      \fi
  }%
}
\def\twocols#1#2{%
    \par
    \hbox to\linewidth
    {%
      \singlebox{#1}\hfill\singlebox{#2}%
    }%
    \prevdepth = 0pt
}

\twocols
  {This is a very long line that will run over over over over over over jjjjj over}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

More text.

\twocols
  {This is a very long line that will run over over over over over over jjjjj 
  over $\Bigg|$}
  {What is wrong with the spacing?}
\twocols
  {What is wrong with the spacing?}
  {This is a very long line that will run over}

Still more text.

% {\tracingall\showlists}

\bye

The output produced by this amended code is

Output of amended code

Please note that this does work at the bottom of pages too. Moreover, since the OP didn’t insert the \parskip glue above the \twocols construction, I did not too; if desired, an explicit \vskip\parskip can be added just before the \hbox command in the definition of \twocols.

GuM
  • 21,558
  • 1
    Try with $\Bigg|$ at the end of the very first box and you'll have a surprise. – egreg Feb 22 '16 at 09:56
  • @egreg: Ah, yes, of course: I was not even trying to cope with a situation like that. Mine was just an elaboration over jfbu’s answer. – GuM Feb 22 '16 at 10:09
  • Good approach with \prevdepth. Solves the issue of behaviour at page bottom from my imaginative proposal. I think egreg is taking a nap. Else he would be asking about why your method does not inserted \parskip glue as it arguably should between two \twocols. Ah yes, his answer does not either. Nobody is perfect. Especially when the world is open ended and answers matter more than questions. –  Feb 22 '16 at 13:35
  • @jfbu: I think egreg has been silenced by the \maxdepth trick… ;-) – GuM Feb 22 '16 at 22:42