4

I am trying to use latex to write my thesis, and I have modified a template to use, but am having trouble with some of my section headings. Specifically, with the weird spacing between words caused by the section heading being justified, rather than flush left.

I am using the titlesec package and have tried:

\titleformat{\section}{\raggedright\large\sffamily\bf}{\thesection}{1em}{}

I also tried forcing titlesec to make everything raggedright by adding

\usepackage[raggedright]{titlesec} 

to my preamble but neither seem to have any effect and the heading text is still strangely spaced. I think it is probably something else within my settings that is taking preference?

\documentclass[11 pt]{report}
%-----------------------------------------
%                  Packages
%-----------------------------------------

\usepackage{titlesec}
\usepackage[a4paper, scale=1.0, textwidth=145mm, textheight=237mm, layoutvoffset=0pt, layouthoffset=0pt, ignoremp, includehead, marginparsep=0pt, bottom=4cm, top=2cm, left=4cm, right=2.5cm, verbose=true, bindingoffset=0pt]{geometry} 
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage[english]{babel} 
\usepackage{setspace} 
\usepackage[toc, page, header]{appendix} 
\usepackage{fancyhdr} 
\usepackage[plain]{fancyref} 
\usepackage{textcomp} 
\usepackage{sectsty}
\usepackage{balance} 
\usepackage{lastpage}
\usepackage[format=plain,justification=centering,singlelinecheck=false,font=small,labelfont=bf,labelsep=space]{caption} 

%-----------------------------------------------------------------------
%   MAIN PAGE SETUP
%-----------------------------------------------------------------------

\renewcommand{\rmdefault}{ptm} % sets roman font to Times ('ptm')
\renewcommand{\sfdefault}{phv} % sets serif font to Helvetica 
\renewcommand{\ttdefault}{lmtt} % sets text type font to Latin Modern Typewriter ('ptm')
\widowpenalty=500 
\clubpenalty=500 
\pretolerance=10000 
\tolerance=2000 %
\emergencystretch=10pt 

\titleformat{\section}{\fillleft\large\sffamily\bf}{\thesection}{1em}{} %formats the section titles 

\begin{document}

\onehalfspace
\chapter[Introduction]{Introduction}
\chaptermark{Introduction}
\label{ch:introduction} % label for referring to chapter in other parts of the thesis
\section[Importance]{Importance}\label{C1:Intro}

Filling in later

 \section[Synthesis]{Synthesis of averylongcompoundnamethatis toobigtofitinthisbox}

 blahblahblah

\end{document}

Longtitleimage

  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Your document is so long, that is hard to tell where the error might be – user31729 Jun 11 '14 at 6:38
  • Hi. Yeah I know, I think this is where I am having trouble as well. I will try and cut parts out and see if they have an effect. – Dylan Jun 11 '14 at 6:45
  • 1
    I think, it is the \pretolerance Please turn it off by adding a % before! – user31729 Jun 11 '14 at 6:47
  • 1
    Do you really want hyphenation turned off for the whole thesis? – Andrew Swann Jun 11 '14 at 6:54
  • 2
    If you don't want hyphenation you should set the whole document raggedright, justifying paragraphs with no hyphenation will make a horrible result with over-stretched space – David Carlisle Jun 11 '14 at 7:32
3

You are already loading package sectsty. Then use

\sectionfont{\raggedright\large\sffamily\bfseries}

or do not load package secsty and \titleformat will also work. If you want to allow hyphenation then use:

\usepackage{ragged2e}
\sectionfont{\RaggedRight\large\sffamily\bfseries}
| improve this answer | |
  • Yes, this seems to do what I was asking, how come this works while the titlesec seemed to just ignore me? – Dylan Jun 11 '14 at 7:11
  • 1
    Do not load package secsty and titlesecwill also work. – user2478 Jun 11 '14 at 7:14
  • @user2478, can you spell that out for us? We want to learn how this works. – Merchako Apr 27 at 20:38
2

The \pretolerance value is too large, preventing a hyphenation and its consequent line break and therefore stretches the line to break at the next possible position, i.e. at toobig... The problem vanishes after turning it off (by commenting it)

\documentclass[11pt]{report}

\usepackage[a4paper, scale=1.0, textwidth=145mm, textheight=237mm, layoutvoffset=0pt, layouthoffset=0pt, ignoremp, includehead, marginparsep=0pt, bottom=4cm, top=2cm, left=4cm, right=2.5cm, verbose=true, bindingoffset=0pt]{geometry}
\usepackage{titlesec}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage[english]{babel} 
\usepackage{setspace} 
\usepackage[toc, page, header]{appendix} 
\usepackage{fancyhdr} 
\usepackage[super,sort&compress,comma]{natbib} 
\usepackage[plain]{fancyref} 
\usepackage[version=3]{mhchem}
\usepackage{times,mathptmx}
\usepackage{textcomp} 
\usepackage{sectsty}
\usepackage{balance} 
\usepackage{graphicx} 
\usepackage{lastpage}
\usepackage[format=plain,justification=centering,singlelinecheck=false,font=small,labelfont=bf,labelsep=space]{caption} 

\newcommand{\mk}[1]{%
%
}


%-----------------------------------------------------------------------
%   MAIN PAGE SETUP
%-----------------------------------------------------------------------

% This section is relevant to formatting within chapters
\linespread{1.8} %1.5 line spacing
\renewcommand{\rmdefault}{ptm} % sets roman font to Times ('ptm')
\renewcommand{\sfdefault}{phv} % sets serif font to Helvetica 
\renewcommand{\ttdefault}{lmtt} % sets text type font to Latin Modern Typewriter ('ptm')
\pagestyle{fancyplain} % retains control of headers and footers
\fancyhf{}
\fancyheadoffset{0.0in} % margin size at header
\renewcommand{\headrulewidth}{0.4pt} % thickness of rule in header line (0pt will make it disappear)
\lhead{\fancyplain{}{}}
\rhead{\fancyplain{\leftmark}{\leftmark}} %prints current chapter name in the right header
\fancyfoot[R]{\thepage}
\chead{\fancyplain{}{}}
\renewcommand{\footrulewidth}{0.4pt} % thickness of rule in footer line

\headsep=12pt % Sets distance between header rule and text
\parindent=12pt % Sets indentation for each paragraph
\parskip=0.1in % Sets vertical space before a new paragraph
\widowpenalty=500 % elliminates widows (single lines of a paragraph on the top of the next page), see line below
\clubpenalty=500 % these two lines eliminate widows and orphans from the document, but may affect typesetting quality elsewhere.

%\pretolerance=10000 % turns off hyphenation

\tolerance=2000 % turns off hyphenation
\emergencystretch=10pt % turns off hyphenation


%  The following changes the Chapter Title format 

\titleformat{\section}{\raggedright\large\sffamily\bf}{\thesection}{1em}{} %formats the section titles 

% for a bit more fancy chapter and section titles use the following 12 lines:
\renewcommand{\thechapter}{\arabic{chapter}} 
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filleft\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{4ex}
{\titlerule
\vspace{2ex}%
\filright
}%
[\vspace{2ex}%
\titlerule]
\renewcommand{\captionfont}{\sffamily}{\small}
\renewcommand{\captionlabelfont}{\bf}

\begin{document}

\onehalfspace
\chapter[Introduction]{Introduction}
\chaptermark{Introduction}
\label{ch:introduction} % label for referring to chapter in other parts of the thesis
\section[Importance]{Importance}\label{C1:Intro}
 Filling in later

 \section[Synthesis]{Synthesis of averylongcompoundnamethatis toobigtofitinthisbox}

 blahblahblah



\end{document}
| improve this answer | |
  • Yeah, but this then causes it to hyphenate halfway through the word - which I was trying to avoid. I would rather the line break between the words and have it all pushed to the left if possible? – Dylan Jun 11 '14 at 6:55
  • You can use another value of \pretolerance at an appropiate place mid-document – user31729 Jun 11 '14 at 6:56
  • Ok, but then what if I use another compound with a long name later on? – Dylan Jun 11 '14 at 7:04
  • Is there a more appropriate value to use for the pretolerance? Or does latex generally handle these ok? Sometimes they can be placed strangely. – Dylan Jun 11 '14 at 7:14
  • Honestly spoken: In nearly 20 years of TeXing I was never in need of changing \pretolerance, so LaTeX handles it in most cases correctly ;-) – user31729 Jun 11 '14 at 7:17

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.