Text FormattingFormatting \part command

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Formatting \part command

Post by svend_tveskaeg »

Hi all.

Consider the following example:

Code: Select all

\documentclass{article}

\begin{document}

\part{Beskrivelse af elever og l{\ae}ringsmilj{\o}, baseret p{\aa} klasserumsstudier}
Some random text.

\end{document}
(1) How do I get the ``Part I'', produced just before the text in the \part argument, to be horizontically centred on the page? Not the text in the argument but just ``Part I''.

(2) Is it possible to hyphanate a word in a \part argument? If so, how do I do it?

If you can help me with `only' one of my two problems, I would still very much like to hear from you.

Thank you in advance!
Last edited by svend_tveskaeg on Fri Mar 04, 2011 6:52 pm, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Formatting \part command

Post by localghost »

At this point I usually recommend the titlesec package. But in this case this won't help because the package doesn't support formatting the \part command. So we have to pick up the corresponding code from the »article« class and modify it (see code below).

From the typographical point of view it is not a good idea to have hyphenation in headings. And the definition of the \part command respects that as it includes \raggedright. However, you can dot it (see comments in the code below). But the result in this case is still a part title with no hyphenation.

Code: Select all

\documentclass[11pt,a4paper,danish]{article}
\usepackage[T1]{fontenc}      % enable Cork Encoding
\usepackage[utf8]{inputenc}   % Input encoding for direct insertion of special national characters
\usepackage{babel}

\makeatletter
\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    {\parindent \z@ %\raggedright                    % Commented \raggedright
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >\m@ne
       \Large\bfseries\hfil\partname~\thepart\hfil   %  added \hfil … \hfil for centering
       \par\nobreak
     \fi
     \huge \bfseries #2%
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}
\makeatother

\begin{document}
  \part{Beskrivelse af elever og læringsmiljø, baseret p{\aa} klasserumsstudier}
\end{document}
Finally some remarks not related to the problem. With Danish you are writing in a European language. So you should turn on the right font encoding to improve hyphenation (see code above). And I guess that in your actual document you turned on the right language by babel for correct hyphenation patterns.


Thorsten
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Formatting \part command

Post by svend_tveskaeg »

Thank you very much, Thorsten! This is exactly what I want.

PS. You are correct; I do have the correct font encoding in my `real' document.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Formatting \part command

Post by svend_tveskaeg »

When Thorsten mentioned the titlesec package, I got the idea of letting people know how I normally configure my \section -- \subsubsection titles. Load the aforementioned package and put the following in the preamble:

Code: Select all

\titleformat\section{\bfseries\sffamily\filleft\LARGE}{\thesection}{}{}
\titleformat\subsection{\bfseries\sffamily\filleft\large}{\thesubsection}{}{}
\titleformat\subsubsection{\bfseries\sffamily\filleft}{\thesubsubsection}{}{}
Maybe it is helpful to someone.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply