Calculate the lengths of rules (in LaTeX3?)

I don't think this hard to do but I 'am a real noob with calculations and dimensions.

In the code above I would like to calculate automatically the dimensions of the left and right rules such as leaving 20% of the line width unruled on both left and right side. If a LaTeX3 version is possible, it would be great for me.

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\usepackage[showframe]{geometry}

\newcommand\docruler[1]{%
\begin{center}
\color{green!50!black}%
\scriptsize%
\itshape\bfseries%
\rule{.25\linewidth}{2.5pt}% <-- Calc. here.
\,\,#1\,\,%
\rule{.25\linewidth}{2.5pt}% <-- Calc. here.
\end{center}
}

\begin{document}

\docruler{ABCDEFGH}

\docruler{ABCDEFGH}

\docruler{XYZ}

\end{document}


The following explicitly calculates the widths by first typesetting the argument into a box and measuring that box.

I've loaded the showframe package just so one can estimate the 20% of the line width being free.

\documentclass[]{article}

\usepackage{showframe}

\ExplSyntaxOn
\box_new:N \l_projetmbc_tmpa_box
\dim_new:N \l_projetmbc_rule_dim
\NewDocumentCommand \docruler { m }
{
\begin {center}
\scriptsize\itshape\bfseries
\hbox_set:Nn \l_projetmbc_tmpa_box { \,\, #1 \,\, }
\dim_set:Nn \l_projetmbc_rule_dim
{ .3\linewidth - .5\box_wd:N \l_projetmbc_tmpa_box }
\rule { \dim_use:N \l_projetmbc_rule_dim } { 2.5pt }
\box_use:N \l_projetmbc_tmpa_box
\rule { \dim_use:N \l_projetmbc_rule_dim } { 2.5pt }
\end {center}
}
\ExplSyntaxOff

\begin{document}
\docruler{ABCDEFGH}

\docruler{ABCDEFGH}

\docruler{XYZ}
\end{document}


• Thanks for this LaTeX3 version. What is the advantage to use showframe? Jul 12 at 14:26
• @projetmbc you see the page margins in form of a frame drawn on the paper. That's the only useful thing the package does. Jul 12 at 14:27
• I code too much today and my brain is melting... I have to go outside before asking another "stupid" question. :-) Thanks for all! Jul 12 at 14:29