# insert text in rule

I have difficulty writing a compact command that allows me to insert a text between two lines. I read some links such as this and this, but each one treats the left and right line separately

\documentclass{article}
\usepackage{amsmath} %<<===
\usepackage[normalem]{ulem}
\usepackage{xcolor}

\newcommand\gsout{\bgroup\markoverwith{\textcolor{gray}{\rule[0.5ex]{2pt}{2.5pt}}}\ULon}

\newcommand*{\sxrule}[1][3em]{\textcolor{gray}{\rule[0.5ex]{#1}{2.5pt}}\;}

\begin{document}

\sxrule Osservazioni \gsout{\hfill}

\end{document}


Have any suggestions? thanks

• Should it be on a separate line? – Bernard Nov 28 '18 at 20:36
• @Bernard a newcommand '\rule\text {...}\rule' – Antonio Nov 28 '18 at 20:42
• I don't see exactly what you're trying to obtain. Could you explain more, or post a hand-made sketch? – Bernard Nov 28 '18 at 20:47
• I can't compile this code – there's an error messageright after \sxrule. – Bernard Nov 29 '18 at 0:10
• The compilation error in the MWE is caused by \; as this is math-mode spacing so it needs to typed as $\;$. – user30471 Nov 29 '18 at 9:15

Perhaps something like this:

Here's the code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}

\newlength\sxrulelength
\newcommand\sxrule[2][3em]{%
\settowidth\sxrulelength{\,#2\,}%
\noindent%
\textcolor{gray}{\rule[0.5ex]{#1}{2.5pt}}\,#2\,%
\textcolor{gray}{\rule[0.5ex]{\dimexpr\linewidth-\sxrulelength-#1\relax}{2.5pt}}%
}

\begin{document}

\lipsum

\sxrule{Osservazioni}

\lipsum

\end{document}


The \sxrule macro has an optional argument that controls the length of the "left hand" line. As in the OP, this defaults to 3em.

Judging from the comments, perhaps the definition

\newcommand\sxrule[2][3em]{%
\settowidth\sxrulelength{\,#2\,}%
\noindent%
\textcolor{gray}{\rule[0.5ex]{#1}{2.5pt}}\,\textbf{#2}\,%
\textcolor{gray}{\rule[0.5ex]{\dimexpr\linewidth-\sxrulelength-#1\relax}{2.5pt}}%
}


is better as now \sxrule{Osservazioni} puts "Osservazioni" in bold:

• interesting solution, I was trying with this but I was wrong. The color of the text and lines must be independent. The line must be gray (or another color), while the text must be black – Antonio Nov 29 '18 at 6:06
• I found another problem. if I use this command, all the text under the command stays gray : \sxruledx{\textbf{Osservazioni}}\\ \lipsum[3] – Antonio Nov 29 '18 at 6:26
• @Antonio See my edit to the colour of the text in the midle of the lines (sorry, I thought that you wanted it gray as well:). As above, the colour after the \sxrule should be black, not, gray. Finally, use a blank line instead of \\  after the \sxrule...alternatively make this part of the macro definition. – user30471 Nov 29 '18 at 9:19
• my alternative solution : \newlength\sxrulelength \newcommand\sxruledx[2][2.5em]{\color{black}\vspace{0.25cm}% \settowidth\sxrulelength{$\,$#2$\,$}% \noindent% \textcolor{gray}{\rule[0.5ex]{#1}{1.5pt}\textcolor{blue}{$\,$#2$\,$}% \rule[0.5ex]{\dimexpr\linewidth-\sxrulelength-#1\relax}{1.5pt}{}\vspace{0.25cm}}% } – Antonio Nov 29 '18 at 17:52

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{xcolor,lipsum}
\hrule height .7ex depth -.4ex  \hfill }\hskip .0em}}

If you do not want the word centered but start at a fixed point, change the first \filrule in the definition of \ruledworld by a normal \rule. Or You can left a single \filrule on the left, but a few on the right (e.g., \filrule\filrule\filrule) to have the word centered with respect some point (at the 25% of the line, in that example).
• if I enter \rule, I get this error : Undefined control sequence. \ruledworld{Osservazioni} and Command \rule already defined. ...ht .7ex depth -.4ex \hfill }\hskip .0em}} – Antonio Nov 29 '18 at 5:59
• @Antonio Sorry, I mean "\rule" with their mandatory and optional arguments, just as you used in your minimal working example (e.g . \rule[0.5ex]{2pt}{2.5pt}) but obviously changing these arguments as you need. For example: \rule[.4ex]{1em}{.3ex} – Fran Nov 29 '18 at 9:33
• @Antonio And obviously you need to take care also of add a gray color for this rule, as the color was defined only inside the \filrule definition. – Fran Nov 29 '18 at 9:43