Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I change the following macro to get a rule in hbox 2 that is just as wide as the text in hbox 1 and remove parameter #2?

\def\m#1#2{
   \leavevmode
   \hbox to#2{
      \hbox to#2{\hfil\lower9pt\hbox{\it#1}\hfil}% hbox 1
      \hss
      \hbox to#2{\leaders\hrule\hfil}}}% hbox 2

P.S. Solutions just with TeX and plain TeX are preferred.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

There is no need for manual measuring. \hrule automatically extends to the width of the surrounding vertical box (if the parameter width is not given):

\def\m#1{%
  \leavevmode
  \vtop{%
    \hrule
    \kern2pt % vertical space between rule and text
    \hbox{\it#1\/}%
  }%
}
abc\m{foo bar}def\m{man}ghi
\bye

Result

Or if the distance between rule and text base line should be constant.

\def\m#1{%
  \leavevmode
  \vtop{%
    \hrule
    \vbox to .7\baselineskip{%
      \vfill
      \hbox{\it#1\/}%
    }%
  }%
}
abc\m{foo}def\m{man}ghi\m{gpq}jkm
\bye

Result

Remark:

  • Both examples add an italics correction at the end of the text.
share|improve this answer
    
Why does this solution need the \leavevmode command? –  Angel Tsankov Jun 9 at 14:07
    
@AngelTsankov: \leavevmode is inherited from the question. You can also use it without. Then it does not start a paragraph in vertical mode. –  Heiko Oberdiek Jun 9 at 14:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.