6

Looks like inside {varwidth} hangindent is applied to the right side of the paragraph, instead of the left side:

\documentclass{article}
\usepackage{varwidth}
\usepackage{lipsum}

\begin{document}

\noindent
\begin{minipage}{\linewidth}
\setlength{\hangindent}{15pt}
\lipsum[1]
\end{minipage}

\noindent
\begin{varwidth}{\linewidth}
\setlength{\hangindent}{15pt}
\lipsum[1]
\end{varwidth}

\end{document}

Screenshot

Any solutions?

  • there is also not enough vertical space between the two paragraphs. this is a known deficiency in the handling of adjacent vertical boxes, and can be corrected by the addition of a \strut at the end of the upper and beginning of the lower box. (it would be even worse if there were no descender in the last line of the upper paragraph.) – barbara beeton Apr 7 '15 at 15:36
5

varwidth builds a \vbox and then dismantles it for measuring the widest line, for later resetting it. When lines are measured, the information about the hanging indentation is lost.

If I say

\setbox0=\vbox{
  \hangindent=15pt
  \lipsum[1]
}{\showboxdepth=1 \showboxbreadth=1000 \showbox0 }

I get in the log

> \box0=
\vbox(138.94444+1.94444)x345.0
.\hbox(6.94444+1.94444)x345.0, glue set 0.85849 []
.\penalty 150
.\glue(\baselineskip) 3.11111
.\hbox(6.94444+1.94444)x330.0, glue set - 0.22638, shifted 15.0 []
.\glue(\baselineskip) 3.11111
.\hbox(6.94444+1.94444)x330.0, glue set 1.06665, shifted 15.0 []
.\glue(\baselineskip) 3.11111
.\hbox(6.94444+1.94444)x330.0, glue set 0.26152, shifted 15.0 []
.\penalty 100
.\glue(\baselineskip) 3.11111
.\hbox(6.94444+1.94444)x330.0, glue set - 0.24269, shifted 15.0 []
.\glue(\baselineskip) 3.11111
.\hbox(6.94444+1.94444)x330.0, glue set 0.34998, shifted 15.0 []
.\glue(\baselineskip) 3.11111
[...similar lines omitted...]

so you see that hanging indentation is obtained by shifting boxes, rather than adding glue to their left. When you have such a box and apply \lastbox to it (which is what varwidth does in its measuring stage), the information the box was shifted is lost and cannot be recovered in any way.

You can emulate hanging indent with \leftskip. However, I'm not sure why you're using varwidth in this case.

\documentclass{article}
\usepackage{varwidth}
\usepackage{lipsum}

\begin{document}

\noindent
\begin{minipage}{\linewidth}
\setlength{\hangindent}{15pt}
\lipsum[1]
\end{minipage}

\medskip % to make barbara happier

\noindent
\begin{varwidth}{\linewidth}
\setlength{\leftskip}{15pt}
\hspace*{-15pt}\lipsum[1]
\end{varwidth}

\end{document}

enter image description here

| improve this answer | |
  • Thank you for the explanation. I was just searching for a simple solution to refactor {minipage} to {varwidth} in a very complex latex package. I guess I'll better get rid of \hangindent. Messing with \lineskip seems dangerous. – Desik Apr 7 '15 at 14:36
  • @DenisBogdanas Also messing with \hangindent in LaTeX is dangerous: list environments use \parshape that overrides \hangindent. – egreg Apr 7 '15 at 14:37
  • please add a \strut or two to fix the bad spacing between the two boxes. i would not expect the problem since there is a descender in the last line of the first paragraph, but it's quite visible. – barbara beeton Apr 7 '15 at 15:41
  • @barbarabeeton That was just to show the result. – egreg Apr 7 '15 at 15:53
  • @egreg -- oh, okay. it's just that things like this drive me crazy. – barbara beeton Apr 7 '15 at 16:16

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.