2

I am trying to add a horizontal line (like \hrule) that starts just after each section's heading, fills the rest of the line, and is vertically aligned with the heading, as shown below. Desired output

To do so, I have defined a \newcommand called \raisedrule that generates the desired horizontal line (following this post). However, I am having difficulty incorporating it into the definition of the sections' headings.

I have tried using the titlesec package, but I have not found a way to generate a line that is shorter than the text's width. I have also tried using the sectsty package, using the following code:

\documentclass{article}

\newcommand{\raisedrule}[2][0em]{\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill}

\usepackage{sectsty} 
    \sectionfont{
        \setmainfont{Arial} \raisedrule[0.32em]{0.75pt}
    }

\usepackage{fontspec}

\begin{document}

\section*{SECTION HEADING}

\end{document}

which, not only gives a "Leaders not followed by proper glue." error, but I don't think would generated the required output anyway.

Note that, as shown in the code, I also want to change the font of the heading, which may be important for the solution.

Any help would be greatly appreciated.

  • \hrulefill will come in handy here – Sean Allred Sep 19 '15 at 15:40
  • @SeanAllred I cannot see images on Imgur right now, could you please describe to me the vertical position of the desired rule? I'm not sure if is as in my answer or if a simple \hrulefill is enough. – Gonzalo Medina Sep 19 '15 at 15:49
  • @GonzaloMedina It's a hair under 1ex – Sean Allred Sep 19 '15 at 15:50
1

If you are willing to switch to titlesec which offers more customization capabilities (I also used xhfill to easily produce the rule, but this is optional):

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec} 
\usepackage{xhfill}

\newcommand\ruleafter[1]{#1~\xrfill[.7ex]{1pt}}
\titleformat{\section}
  {\normalfont\Large\fontspec{Arial}}{\thesection}{1em}{\ruleafter}

\begin{document}

\section*{SECTION HEADING}

\end{document}

The result:

enter image description here

Adjust the rule settings according to your needs; you might want to adjust the space between the title and the rule and, as Sean Allred mentions in his comment, it's enough to use \fontspec instead of \setmainfont.

  • 1
    Nice tip about \xrfill :) I was having trouble with a \raisebox{0.8ex}{\hrulefill} solution. I'd stick a \quad in there for aesthetic, though. And I wouldn't use \setmainfont there; a simple \fontspec would suffice. – Sean Allred Sep 19 '15 at 15:52
  • @SeanAllred Yes. I added some remark about adding some space and changed to \fontspec. – Gonzalo Medina Sep 19 '15 at 15:54

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.