Underline and center headings

Building on Gonzalo's answer: Use the numberless key to define the starred version of \section.

\documentclass{scrartcl}

\usepackage[explicit]{titlesec}
\usepackage[normalem]{ulem}
\usepackage{lipsum}

\titleformat{\section}
  {\normalfont\Large\sffamily\bfseries\filcenter}{}{0em}{\uline{\thesection\hspace*{ 1em}\MakeUppercase{#1}}}
\titleformat{name=\section,numberless}
  {\normalfont\Large\sffamily\bfseries\filcenter}{}{0em}{\uline{\MakeUppercase{#1}}}

\renewcommand*{\thesection}{\Roman{section}}

\begin{document}

\section{Test Section}

\lipsum[1]

\begin{thebibliography}{9}
\bibitem{A01} A. Author. \emph{Alpha}. 2001.
\end{thebibliography}

\end{document}

enter image description here


Here's a simple solution using the ulem package and the explicit option for titlesec:

\documentclass{scrartcl}

\usepackage[explicit]{titlesec}
\usepackage[normalem]{ulem}
\usepackage{lipsum}

\titleformat{\section}
  {\normalfont\Large\sffamily\bfseries\filcenter}{\uline{\thesection\hspace*{ 1em}}}{0em}{\uline{\MakeUppercase{#1}}}

\renewcommand*{\thesection}{\Roman{section}}

\begin{document}
\section{Test Section}

\lipsum[1]

\begin{thebibliography}{9}
\bibitem{A01} A. Author. \emph{Alpha}. 2001.
\end{thebibliography}

\end{document}

enter image description here


Because both the answer provided by Gonzalo and the answer provided by lockstep don't work well, here is mine. This might be grave diggin' because the original question is 5 years old but it shows up at the top of google. The problem with both of their solutions is, that the entire caption is in one hbox. Because of that long captions do not work. They get an overfull hbox. My solution only addresses that and doesn't address the rest of the question (capitalized captions and stuff). In addition my solution does not work with KOMA-script-classes.

\documentclass{article}

\usepackage[normalem]{ulem}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{sectsty}

\allsectionsfont{\sffamily\underline}
\titleformat{\section}
{\normalsize\bfseries\raggedright}
   {\llap{\makebox[3mm][l]{\thesection}}}
   {0em}
   {}

\begin{document}
\section{This is a really long section heading and I'm proud of it}
\lipsum[1]
\end{document}

EDIT: For a KOMA compatible way to allow line breakable underlined headings see this answer.

EDIT2: If you use KOMA and hyperref, the above provided link doesn't work if you use \tableofcontents or similar, so you might take a look at this other answer.