[Tex/LaTex] LaTeX Underlined section titles with note to right without underline

sectioning

I would like to have underlined section titles. However, I would like to put a reference to the right, without extending the underline completely. I am using sectsty and ulem to get the underlined section. Below is an example of what I am trying to do, but the underline continues to the right.

\documentclass[12pt]{article}
\usepackage{sectsty}
\usepackage[normalem]{ulem}

\sectionfont{\ulemheading{\uuline}}

\begin{document}
    \section{Section Title\hfill [Ref]}
\end{document}

Best Answer

I would second what @egreg said about avoiding the need for underlining. But sometimes it can't be helped (like when you're working with others who insist on particular styling). So I'll help you to get where you want to be.

This solution allows a full [double] underline (just like with the plain solution you had in your MWE), but also gives an alternate option for the reference.

I have redefined things to allow you to continue to use \section{...} and \section*{...} as normal. But now, instead of changing the TOC line, the optional argument is used for the reference:

\section[{[ref]}]{Title}
\section[\cite{articleABC}]{Title}

A full example, with the appropriate redefinitions:

\documentclass[12pt]{article}
\usepackage{sectsty}
\usepackage[normalem]{ulem}
\usepackage{showframe} % For illustration

% Add reference functionality
\makeatletter
\sectionfont{\ulemheading{\uuline}}
\let\oldsection\section
\def\section{\@ifstar\s@section\@section}
\newcommand{\@section}[2][\relax]{\oldsection{\llap{\rlap{\hspace{\textwidth}\llap{#1}}\protect\phantom{\thesection\quad}}#2}}
\newcommand{\s@section}[2][\relax]{\oldsection*{\llap{\rlap{\hspace{\textwidth}\llap{#1}}}#2}}
\makeatother

\begin{document}
    \section[\cite{test}]{Section Title}  Regular section
    \section*[{[1]}]{Section Title} Starred section % Manually give reference
    \section{Section Title} Without reference
    \section*{Section Title} Without reference, starred section
    \setcounter{section}{100}
    \section[\cite{test}]{Section Title} With a large section number
    \section[\cite{test}]{This is a Very, Very, Very Long\\Section Title} With a very long title, you must manually break the line to avoid overlapping the reference
\end{document}

This will make the following sections:

examples

Related Question