[Tex/LaTex] Translating custom \section format to titlesec

formattingindentationsectioningspacingtitlesec

I have some custom \section formatting with a horizontal rule, and I'm trying to DRY out my code by using titlesec to define default formatting options. But I can't exactly reproduce what happens when not using titlesec. Here's an example:

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{xhfill}

\begin{document}

% Plain xrfill:
\section*{\large\normalfont First Section \xrfill[0.6ex]{0.4pt}}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

% Via titlesec:
\titleformat{\section}[runin]{\large}{\thetitle}{1em}{#1 \xrfill[0.6ex]{0.4pt}}
\section*{Second Section (\texttt{titlesec})}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

% Titlesec with a \\ at the end:
\titleformat{\section}[runin]{\large}{\thetitle}{1em}{#1 \xrfill[0.6ex]{0.4pt}\\}
\section*{Third Section (line break)}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\end{document}

Note that using titlesec doesn't put the following text in a new paragraph. I tried to solve this with \\ but that doesn't work either. What can I do to make this work the same way as it would without titlesec?

Best Answer

Use the [display] style instead of the [runin] style:

\titleformat{\section}[display]{\large}{\thetitle}{1em}{#1\space\xrfill[0.6ex]{0.4pt}}
\section*{Second Section (\texttt{titlesec})}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

enter image description here