2

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} 

enter image description here

Have any suggestions? thanks

8
  • Should it be on a separate line?
    – Bernard
    Commented Nov 28, 2018 at 20:36
  • @Bernard a newcommand '\rule\text {...}\rule'
    – Antonio
    Commented Nov 28, 2018 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
    Commented Nov 28, 2018 at 20:47
  • 1
    I can't compile this code – there's an error messageright after \sxrule.
    – Bernard
    Commented Nov 29, 2018 at 0:10
  • 1
    The compilation error in the MWE is caused by \; as this is math-mode spacing so it needs to typed as $\;$.
    – user30471
    Commented Nov 29, 2018 at 9:15

2 Answers 2

2

Perhaps something like this:

enter image description here

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:

enter image description here

4
  • 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
    Commented Nov 29, 2018 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
    Commented Nov 29, 2018 at 6:26
  • 1
    @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
    Commented Nov 29, 2018 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
    Commented Nov 29, 2018 at 17:52
2

mwe

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{xcolor,lipsum}
\newcommand\filrule{{\color{gray}{\leaders
\hrule height .7ex depth -.4ex  \hfill }\hskip .0em}}
\def\ruledworld#1{\bigskip\noindent\filrule\quad%
#1\quad\filrule\bigskip\par}
\begin{document}
\ruledworld{Osservazioni}
\lipsum[2]
\ruledworld{Grandi conclusioni}
\lipsum[3]
\end{document} 

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).

3
  • 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
    Commented Nov 29, 2018 at 5:59
  • 1
    @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
    Commented Nov 29, 2018 at 9:33
  • 1
    @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
    Commented Nov 29, 2018 at 9:43

You must log in to answer this question.

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