我有以下自定义小节标题格式:
\documentclass{article}
% Raised Rule Command:
% - arg 1 (optional) how high to raise the rule
% - arg 2 thickness of the rule
\newcommand{\raisedrulefill}[2][0ex]{\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill}
\usepackage[explicit]{titlesec}
\titleformat{\subsection}{\large\bfseries}
{\rule[0.3ex]{5pt}{1.5pt}
\thesubsection\,\rule[0.3ex]{8pt}{1.5pt}\,}{0em}
{#1\,\raisedrulefill[0.3ex]{1.5pt}}
\begin{document}
\section{Test}
\subsection{This is a test tile}
\subsection{This is a long test title that isn't formatted how I'd like it to
be}
\end{document}
如下所示。
不过,我想重新格式化长度为两行的字幕,如下所示。
因此,不知何故,这条线必须相对于包含标题的框的中心放置(该框将标题环绕)。
你知道如何实现这个目标吗?
答案1
这里有一个选项,您可以保存小节标题,sbox
然后控制此框的宽度,如果宽度超过固定长度\maxwidth
,则标题文本将插入到 parbox 内。
\documentclass{article}
\usepackage[explicit]{titlesec}
% Raised Rule Command:
% - arg 1 (optional) how high to raise the rule
% - arg 2 thickness of the rule
\newcommand{\raisedrulefill}[2][0ex]{\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill}
\def\maxwidth{.8\textwidth}
\newsavebox\tempbox
\def\testwidth#1{%
\sbox{\tempbox}{#1}%
\ifdim\wd\tempbox<\maxwidth
\usebox{\tempbox}%
\else
\parbox{\maxwidth}{#1}%
\fi}
\titleformat{\subsection}{\large\bfseries}
{\rule[0.3ex]{5pt}{1.5pt}
\thesubsection\,\rule[0.3ex]{8pt}{1.5pt}\,}{0em}
{\testwidth{#1}\,\raisedrulefill[0.3ex]{1.5pt}}
\begin{document}
\section{Test}
\subsection{This is a test tile}
\subsection{This is a long test title that isn't formatted how I'd like it to
be}
\end{document}