4

I would like to have a command \myTitle{Example} that would draw a rectangle starting after the text "Example" and going up to the right margin of the page.

This is how it would look like.

enter image description here

How can I do?

Colas
  • 6,772
  • 4
  • 46
  • 96

1 Answers1

9

enter image description here

\leaders are your friend here.

\documentclass{article}
\usepackage{color}
\makeatother
\newcommand\myTitle[1]{%
  \par\noindent{\large\bfseries\mbox{#1\ }\textcolor[gray]{.8}{\leaders\vrule height 1ex\hfill}\par}}

\begin{document}

\myTitle{Some Title}

\myTitle{Some Other Title}

\end{document}

David Carlisle
  • 757,742
  • Thanks. How can I control the elevation of the rectangle? I've tried with raisebox but it is not working. – Colas Mar 14 '22 at 11:38
  • @Colas try \leaders\vrule height 2ex depth -1ex\hfill – David Carlisle Mar 14 '22 at 14:47
  • Thanks David. More precisely, I would like the rectangle to be aligned with the baseline. See for instance \myTitle{Some Title with a p}. Thank you if you can help and sorry for the following up questions... – Colas Mar 14 '22 at 15:07
  • 1
    @Colas then use depth 0pt My original was aligned to the baseline unless the letters in the initial word had descenders (which they do not in this case). So the image in my answer has the rule on the baseline, the code in my previous comment lifts it off the baseline by 1ex – David Carlisle Mar 14 '22 at 15:12
  • @Colas Please when you ask a question try and make sure it is complete. You have asked two auxiliary questions that would not have been necessary if your original question had included those constraints. My apologies for being a GOM but it keeps me occupied. – Peter Wilson Mar 14 '22 at 19:25
  • @PeterWilson FYI, that's what I already do. – Colas Mar 14 '22 at 20:57