[Tex/LaTex] How to draw horizontal lines right and left from title

beamerrules

I want a horizontal line right and left from the title. The line width should adjust to the title. So, for example, if the title takes 0.4\textwidth, the horizontal line left from the title, should take 0.3\textwidth, as the line right from the title.

So far, I have this code:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usebackgroundtemplate%
{%
    \includegraphics[width=\paperwidth,height=\paperheight]{/some/picture.png}%
}
\beamertemplatenavigationsymbolsempty
\begin{document}
\frame[c]{%
    \centering
    \Huge{\textbf{\rule{0.1\textwidth}{.4pt} Main title \rule{0.1\textwidth}{.4pt}\\
    subtitle}\\
    not bold text}
}
\end{document}

My problems are:
1) The lines are not vertical in the center of the line, but at the bottom.
2) I have to adjust the width of the lines manually, so, if the title changes, the line-width has to be changed manually.

Can anyone help me out? Tried googling, but did not found, what I needed.

Best Answer

You can use \leaders\hrule\hfill to essentially perform an \hrule in horizontal mode.

I removed the background image from your example code and added a \maintitle macro that performs the formatting you want. I also added a small space on either side. I thought that looked better. enter image description here

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}

\makeatletter
\newcommand\maintitle[1]{%
    \quitvmode
    \hb@xt@\linewidth{%
        \dimen@=1ex
        \advance\dimen@-2pt
        \leaders\hrule \@height1ex \@depth-\dimen@\hfill
        \enskip
        \textbf{#1}%
        \enskip
        \leaders\hrule \@height1ex \@depth-\dimen@\hfill
    }%
}
\makeatother

\beamertemplatenavigationsymbolsempty
\begin{document}
\frame[c]{%
    \centering
    \Huge
    \maintitle{Main title}\\
    \textbf{subtitle}\\
    not bold text
}
\end{document}