6

I want my section titles to be same as the margins of the text. Here is an example of the problem (see title in section 1.1). I want a similar behavior as title (1.2). Any suggestions? What am I missing here?

\documentclass{report}
\usepackage{blindtext}

\begin{document}

\chapter{Hope}

\section{Hope of the Essential Trigonometry TRIGTEST}
\blindtext

\section{Hope of the Essential Trigonometry TRIGOTEST}
\blindtext

\end{document}

enter image description here

HBat
  • 509
  • @cmhughes The "TRIGTEST" in title 1.1 is out of bounds. I want Latex to either divide the word as in title 1.2, or move the word to the second line. – HBat Aug 11 '14 at 20:38
  • It doesn't know how to hyphenate the word. You need to tell it where the word can be broken. – cfr Aug 11 '14 at 20:40
  • 1
    @cfr I see but isn't the behavior in the title 1.1 aberrant? If it doesn't know how to hyphenate it should move it to the next line, instead of exceeding the boundaries of margin? – HBat Aug 11 '14 at 20:45
  • 2
    No. In some cases, TeX ends up letting a line run over because it cannot figure out a better point to break the line. The algorithm TeX is using is not as simple as the one that a word processor uses, for example, where it just moves to the next line if stuff doesn't fit. If you run in draft mode, you will see these places where TeX is 'stuck' as black boxes at the margins where things run over the edge. – cfr Aug 11 '14 at 20:48

2 Answers2

9

The titlesec package is used for this. With the option of raggedright, you can get what you mean. See this code,

\documentclass{report}
\usepackage{blindtext}
\usepackage[raggedright]{titlesec}

\begin{document}
\chapter{Hope}

\section{Hope of the Essential Trigonometry TRIGTEST}
\blindtext

\section{Hope of the Essential Trigonometry TRIGOTEST}
\blindtext

\end{document}

with the desired output in both subsections:

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
5

TeX does not know how to hyphenate the word 'trigtest' so you will need to tell it how to do so in the preamble:

\documentclass{report}
\usepackage{blindtext}
\hyphenation{trig-test}
\begin{document}

\chapter{Hope}

\section{Hope of the Essential Trigonometry TRIGTEST}
\blindtext

\section{Hope of the Essential Trigonometry TRIGOTEST}
\blindtext

\end{document}

Hyphenated headings

cfr
  • 198,882
  • What if I don't want it to hyphenate? How can I force it to move any word to the next line (if it doesn't know the hyphenation) and obey the margin rules strictly? – HBat Aug 11 '14 at 20:49
  • 1
    @HBat Well you said you wanted similar behaviour to the behaviour for section 1.2 which is hyphenated. You could force a line break in a particular case, of course. You almost certainly do not want TeX to 'obey the margin rules strictly' but you could adjust the penalties which determine how 'bad' TeX considers various different outcomes. (For example, tell it that too much white space is less bad to encourage line breaks with under-full lines.) However, this is likely to create more problems unless the context is quite specialised. – cfr Aug 11 '14 at 20:53
  • @HBat See http://tex.stackexchange.com/questions/51263/what-are-penalties-and-which-ones-are-defined. – cfr Aug 11 '14 at 21:00
  • Good to know these. Thanks for the answer. I will go with \linebreak but good to know about \hyphenation{trig-test}. – HBat Aug 11 '14 at 21:03