24

Is there a way to fully stretch one line of text on the full width of the page, without manually setting the letter spacing, no matter how many characters on the line?

7
  • If it is just one line, adding \hfill between each word should produce the output you desire.. Commented Apr 2, 2012 at 20:39
  • 1
    I did not really make that clear, sorry for that, but I am looking for a method that solves this by letter-spacing, not word-spacing. See my comment below
    – grrrbytes
    Commented Apr 2, 2012 at 21:18
  • look at the letterspacing tag: tex.stackexchange.com/questions/tagged/letterspacing Commented Apr 2, 2012 at 21:24
  • tnx, I am aware of that. However, this gives less flexibility in the sense you have to constantly alter the letter-spacing relative to the amount of characters, font, font-size, font-type and width of page. The examples below are quite close to what I want, however, they distribute on word-spacing
    – grrrbytes
    Commented Apr 2, 2012 at 21:29
  • @Mixhael You could make your intent clearer; letterspacing is generally not a good typographical device for getting justified text.
    – egreg
    Commented Apr 2, 2012 at 21:38

5 Answers 5

29

I assume you are referring to the spacing between words (or inter-word spacing) when you reference "a line text".

If the number of characters will always fit on the line, then the optional s-parameter for \makebox alignment inserts enough inter-word spacing stretch to fill the box. If the text is greater than the box width, an overfull \hbox warning is generated:

enter image description here

\documentclass{article}
\setlength{\parindent}{0pt}% Just for this example.
\begin{document}
\makebox[\linewidth][s]{Here is some text.} \par
\makebox[\linewidth][s]{Here is some more text.} \par
\makebox[\linewidth][s]{Here is a whole whack of text, plus some punctuation.} \par
\makebox[\linewidth][s]{Here is a whole whack of text, plus some punctuation, and then some more text.} \par
\makebox[\linewidth][s]{Here is a whole whack of text, plus some punctuation, and then some more text, and nothing else.} \par
\end{document}​

The last line stretches beyond the text margin. In the above minimal working example (MWE), replacing \linewidth with \textwidth would also work.


For inter-letter spacing, the soul package can be of help. You define your own inter-letter, inner and outer spaces via a command \sodef{<cmd>}{<font>}{<inter-letter>}{<inner space>}{<outer space>}:

enter image description here

\documentclass{article}
\usepackage{soul}% http://ctan.org/pkg/soul
\setlength{\parindent}{0pt}% Just for this example.
\begin{document}
\sodef\spaceout{}{0pt plus 1fil}{.4em plus 1fil}{0pt}
\makebox[\linewidth][l]{\spaceout{Here is some text.}} \par
\makebox[\linewidth][l]{\spaceout{Here is some more text.}} \par
\makebox[\linewidth][l]{\spaceout{Here is a whole whack of text, plus some punctuation.}} \par
\makebox[\linewidth][l]{\spaceout{Here is a whole whack of text, plus some punctuation, and then some more text.}} \par
\makebox[\linewidth][l]{\spaceout{Here is a whole whack of text, plus some punctuation, and then some more text, and nothing else.}} \par
\end{document}​

Since I am unfamiliar with this kind of modification, consider this just a guide to get you going. The soul package documentation (section 3 Letter spacing, p 8 onward) is filled with examples.

I'm sure microtype would also be able to facilitate your needs.

3
  • Thanks all. This is exactly what I meant, however: I was looking for a method that spreads text evenly on a horizontal line by increasing the letter-space, instead of the word-space. Is that possible too?
    – grrrbytes
    Commented Apr 2, 2012 at 21:17
  • So, 'exactly' was not the right wording. It is very close to what I want
    – grrrbytes
    Commented Apr 2, 2012 at 21:30
  • Tnx @Werner, will look into the soul package
    – grrrbytes
    Commented Apr 2, 2012 at 21:54
17

spread as a command to a box instruction is built into tex (texbook, p.77):

\documentclass{article}
\begin{document}
\hbox spread \linewidth{Here is some text.}
\end{document}

text spread to line width

0
11

Using the soul package

enter image description here

\documentclass{article}


\usepackage{soul}
\sodef\ugg{}{.4em plus 1fill}{1em plus 2 fill}{2em plus 2fill minus.1em}

\begin{document}


\noindent\ugg{one two three\\four five six}

\end{document}
0
9

if you want to stretch all the letters evenly rather than words you can use:

\makeatletter
\def\spreadeven#1{%
\@tfor\next:=#1\do{%
  \next\hfill
 }%
}

\spreadeven{A fox jumped over the lazy dog}
\madeatother

This uses the LaTeX kernel @tfor to iterate over all the letters and add hfill between them.

2
  • Thanks for that. It almost does it as I wanted it, however it seems not to take spaces into consideration, so spaces won't be doubled. I can't differentiate between words anymore
    – grrrbytes
    Commented Apr 2, 2012 at 21:57
  • @Mixhael I answered this way based on your comments in Werner's answer. i think what you are looking is \so{} using the soul package.
    – yannisl
    Commented Apr 2, 2012 at 22:03
7

In ConTeXt there is the \stretched macro for this purpose. Example:

\starttext
    \stretched{Here is some text}
\stoptext

result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .