0

I have the following custom subsection title formatting:

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

Which looks as follows.

enter image description here

However, I'd like reformat subtitles that happen to be two lines long as follows.

enter image description here

So somehow the line would have to be placed relative to the center of a box which contains the title in a box (that wraps the title around).

Do you know how to achieve this?

0

Here an option, you can save subsection heading in sbox then control the width of this box, if the width exceeds a fixed length \maxwidth then the heading text is inserted within a 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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.