# Custom Title Formatting

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

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

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

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?

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

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