I am quite new to LaTeX, and I am trying to make the section title centered between 2 horizontal lines, all within the same line. What I have now is this. However, I'd like the rules to be longer, but adapting to the length of the section name.


But as I was developing my file, I got issues with some lines overlapping with some section titles. I hope I'm being clear with my problem... Thanks in advance


1 Answer 1


This solution uses a savebox to measure the width of the title.

\usepackage{showframe}% MWE only

  \section*{\rule[0.6ex]{\dimexpr 0.5\textwidth-0.3em-0.5\wd\titlebox}{1pt}%
  \rule[0.6ex]{\dimexpr 0.5\textwidth-0.3em-0.5\wd\titlebox}{1pt}}}

\newsection{A longer title}
\newsection{A much much much longer title}


  • It worked perfectly, thank you very much!
    – Mateo
    Feb 3, 2023 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .