将阿拉伯文章节编号置于对齐的标题文本上方

将阿拉伯文章节编号置于对齐的标题文本上方

考虑一下代码

\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}

产生输出

在此处输入图片描述

评论:由于某种原因,该命令\leftskip\fill\rightskip-\leftskip\parfillskip\stretch{2}%在这里似乎不起作用。

问题:如何将阿拉伯文章节编号置于标题上方的中央(我希望左右对齐 --- 最后一行居中)?我用 编译代码lualatex

谢谢。

答案1

您需要display样式,当然,还要将格式说明添加到\titleformat代码中,而不是添加到章节标题中。

\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}

在此处输入图片描述

相关内容