0

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.

\titleformat{\section}[block]{\LARGE\scshape\centering}{\thesection.}{1em}{}
\newcommand{\newsection}[1]{\section*{\rule{0.25\textwidth}{0.3mm}\hfill#1\hfill\rule{0.25\textwidth}{0.3mm}}}

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

1 Answer 1

0

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

\documentclass{article}
\usepackage{showframe}% MWE only

\newsavebox{\titlebox}
\newcommand{\newsection}[1]{\savebox\titlebox{\LARGE\scshape#1}%
  \section*{\rule[0.6ex]{\dimexpr 0.5\textwidth-0.3em-0.5\wd\titlebox}{1pt}%
  \hspace{0.3em}%
  \usebox\titlebox
  \hspace{0.3em}%
  \rule[0.6ex]{\dimexpr 0.5\textwidth-0.3em-0.5\wd\titlebox}{1pt}}}

\begin{document}
\newsection{Short}
\newsection{A longer title}
\newsection{A much much much longer title}
\end{document}

demo

1
  • 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 .