2

I have a sentence I wanted to spread across a line like the following:

\makebox[\linewidth][s]{XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX}\par

What really happens is because it's a new paragraph with indent, it goes over the margin like the following

enter image description here

So the question is how do I spread the XXX across a line subtracting the indent space? Note that I still want the indent at the beginning.

user1935724
  • 1,339

3 Answers3

3

If you want to keep the \parindent before the box, you need to subtract it from the \linewidth to have a box that fits on the page. Subtracting the width is as simple as saying

\dimexpr\linewidth-\parindent

as in

\documentclass{article}
\usepackage{geometry}
\begin{document}
XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX
XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX\par

\makebox[\dimexpr\linewidth-\parindent][s]{XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX}\par

\end{document}

The \dimexpr "command" (it is a so-called primitive) is nicely explained in this thread, as are most of its cousins. AFAIK their creator is also co-responsible for the Breitenlohner-Freedman bound.

  • actually, can I ask what is exactly \dimexpr – user1935724 May 11 '20 at 06:11
  • 2
    @user1935724 \dimexpr allows us to do simple arithmetics with lengths without introducing an new length. One can build linear combinations of lengths with this. If you want to print the result, use \the\dimexpr...., e.g. \the\dimexpr\linewidth-\parindent will print 415.00462pt in the above document. Likewise, \numexpr allows us to do simple arithmetics with integers. See this post for more details. –  May 11 '20 at 06:15
2

This seems to be the first line of a paragraph, rather than a stand-alone line. On that assumption, I'd say: Don't set the width at all! just put \linebreak at the end of the first-line content. But my assumption is probably not correct, and the answer should NOT be to end the text with \linebreak\par!

Also, the original \noindent suggestion by the hard-to-pin-down feline is fine. Using it means you don't need to calculate width. Just put the indent inside the box

 \noindent\makebox[\linewidth][s]{\indent XXX XXXXX XXXXX ... XXXXX}\par
2

It seems you want a paragraph with the line stretched full width, you can specify that directly by setting \parfillskip (the space added to the last line) to 0pt. There is no need for calculations or explicit boxing.

enter image description here

\documentclass{article}
\usepackage{geometry}
\begin{document}
XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX
XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX

{\parfillskip=0pt
XXX XXXXX XXXXX XXXXX XXXXX XXXXX XX XXX XXX XX XXXXX XXXXX

}

\end{document}
David Carlisle
  • 757,742