1

Consider the code

\documentclass{book}
\usepackage{lipsum}

\usepackage{titlesec}
\titleformat{\section}
  {\sffamily\Large\bfseries}
  {\thesection}{1em}{}
\renewcommand{\thesection}{\arabic{section}}

\begin{document}
\thispagestyle{empty}

\section{\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%
I would like to center the Arabic section number above the section title; Moreover, I would like to both left and right justify the title text---with the last line being perfectly centered.}
\lipsum[1]
\end{document}

which produces the output

enter image description here

Remark: For some reason, the command \leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}% seems to have no effect here.

QUESTION: How may I center the Arabic section number atop the title (which I would like to be both left and right justified---with the last line being centered)? I compile the code with lualatex.

Thank you.

0

1 Answer 1

1

You want display style and, of course, to add the formatting instructions to the \titleformat code, not to the section title.

\documentclass{book}
\usepackage{lipsum}

\usepackage{titlesec}
\titleformat{\section}[display]
  {\sffamily\Large\bfseries}
  {\filcenter\thesection}
  {1ex}
  {\justifycenter}
\newcommand{\justifycenter}[1]{%
  \leftskip=0pt plus 1fil
  \rightskip=0pt plus -1fil
  \parfillskip=0pt plus 2fil
  #1%
}
\renewcommand{\thesection}{\arabic{section}}

\begin{document}

\thispagestyle{empty}

\section{I would like to center the Arabic section number above the section title; 
  Moreover, I would like to both left and right justify the title text---with the 
  last line being perfectly centered.}
\lipsum[1]

\end{document}

enter image description here

1
  • Thank you egreg.
    – DDS
    Jul 20, 2022 at 13:59

You must log in to answer this question.

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