问题

问题

问题

我正在尝试使用 创建精美的节标题titlesec。我需要将节名称放入 中\parbox以便换行。我希望parbox延伸到最右边距,但我不知道如何实现这一点。

平均能量损失

\documentclass[draft]{article}
\pagestyle{empty}
\usepackage[explicit]{titlesec}
\usepackage{xcolor,lipsum}
%% Section
\titleformat{\section}[block]%
  {\normalfont\sffamily}%
  {\colorbox{black}{\parbox[c][17pt][c]{30pt}{%
      \hfil\color{white}\Large\thesection\hfil}}}%
  {0.15cm}%
  {%
   \raisebox{-3pt}{\parbox{10.5cm}{\hspace*{0.7pt}\color{black}\Large\sffamily#1\\[-3.5pt]%
   \color{black}\footnotesize\textbf\dotfill}}%
  }
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{1.3ex plus .2ex}

\begin{document}

\section{Ridiculously overly extermely long section title, that definitely spans several lines.}

\lipsum[2]

\end{document}

输出

在此处输入图片描述

答案1

您需要使用\linewidth-30pt-1.5mm-2\fboxsep-2\fboxrule作为宽度。这将从宽度测量中删除编号框 ( )\parbox的宽度、与(和)相关的边界以及框和标题之间的间隙:30pt\colorbox\framebox2\fboxrule2\fboxsep0.15cm\parbox

在此处输入图片描述

\documentclass[draft]{article}
\pagestyle{empty}
\usepackage[explicit]{titlesec}
\usepackage{xcolor,lipsum}
%% Section
\titleformat{\section}[block]%
  {\normalfont\sffamily}%
  {\colorbox{black}{\parbox[c][17pt][c]{30pt}{%
      \hfil\color{white}\Large\thesection\hfil}}}%
  {0.15cm}%
  {%
   \raisebox{-3pt}{\parbox{\dimexpr\linewidth-30pt-1.5mm-2\fboxrule-2\fboxsep}{\raggedright\color{black}\Large\sffamily#1\\[-3.5pt]%
   \color{black}\footnotesize\textbf\dotfill}}%
  }
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{1.3ex plus .2ex}

\begin{document}

\section{Ridiculously overly extermely long section title, that definitely spans several lines.}

\lipsum[2]

\end{document}

相关内容