1

I have the following code:

\usepackage[T1]{fontenc}

\usepackage{titlesec, blindtext, color}

\definecolor{gray75}{gray}{0.75}

\newcommand{\hsp}{\hspace{20pt}}

\titleformat{\chapter}[hang]{\Huge\bfseries}{\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}

I would like to align the chapter title to the right. Any help? This is my current output:

enter image description here

4
  • Only the title or including the number?
    – Skillmon
    Jul 8, 2017 at 18:23
  • Your \hsp macro doesn't have to be long. You might change the \newcommand to \newcommand*.
    – Skillmon
    Jul 8, 2017 at 18:25
  • Including the number. I'm new at this. What's the difference between \newcommand and \newcommand*?
    – anf
    Jul 8, 2017 at 20:05
  • You might take a look here for an explanation on the difference.
    – Skillmon
    Jul 8, 2017 at 20:54

2 Answers 2

1

Use the dedicated command, \filleft in the second mandatory argument of \titleformat if you want the whole set chapter number+ chapter title to be right aligned:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{titlesec, blindtext, color}

\definecolor{gray75}{gray}{0.75}

\newcommand{\hsp}{\hspace{20pt}}

\titleformat{\chapter}[hang]{\filleft\Huge\bfseries}{\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}

    \begin{document}

    \chapter{Introdução}

\section{As Doenças Cerebrovasculares}
\blindtext

    \end{document}

enter image description here

0
1

The following might give your desired result:

\documentclass[]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{titlesec, blindtext, color, pbox}

\definecolor{gray75}{gray}{0.75}

\newcommand*{\hsp}{\hspace{20pt}}
\newcommand{\titlepbox}[1]{\pbox[t]{0.7\textwidth}{#1}}

\titleformat{\chapter}[block]{\Huge\bfseries}{\hfill\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries\titlepbox}

\begin{document}
\chapter{Foo}
\blinddocument
\end{document}

enter image description here

You might tweak the \titlepbox command (e.g. insert a \raggedleft or \raggedright infront of the #1) for some more options.

Also the maximum width (currently at 0.7\textwidth) of the headings (excluding the number and vertical line) can be changed.

The [t] controls vertical placement -- [b] would make the number bottom aligned to the heading.

0

You must log in to answer this question.

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