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 is the \underline command defined? And how can one make a custom underline to be used just as a horizontal line but which respects a specified height or the height of text, math input, etc and of a specific width?

Here is my first attempt:

\newcommand{\ndfracunderline}[1]{\ensuremath{\underline{\phantom{\dfrac{2}{3\hspace*{#1}}}}}}

This works just fine but the following does not:

\newsavebox\underlinebox

\newcommand\cunderline[3][black]{%
\newlength{\cunderlineheight}%
\settoheight{\cunderlineheight}{#3}%
{\sbox\underlinebox{\cunderlineheight\hspace*{#2}}%
  \rlap{\usebox\underlinebox}\color{#1}\rule[-1.2\dp\underlinebox]{\wd\underlinebox}{0.5pt}}}
share|improve this question
    
LaTeX's \underline is defined in the kernel in the following way: \def\underline#1{\relax \ifmmode\@@underline{#1}\else $\@@underline{\hbox{#1}}\m@th$\relax\fi}, where \@@underline has been previously \let to the TeX's primitive \underline: \let\@@underline\underline. –  Gonzalo Medina Sep 21 '12 at 1:04
    
@GonzaloMedina I got that exactly when i used \show\underline but thought there was another definition because of the use of \@@underline which seemed redundant. –  azetina Sep 21 '12 at 1:16
3  
@azetina: Wait, are you saying that there might be an alternative way -- other than \tikzmark? :-) –  Peter Grill Sep 21 '12 at 3:51
add comment

1 Answer

up vote 6 down vote accepted

I don't know whether this might be what you're after:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\makeatletter
\newcommand{\newunderline}[3][black]{% \newunderline[<colour>]{<width>}{<stuff>}
  {\def\@tempa{!}%
   \def\@tempb{#2}%
   \ifx\@tempa\@tempb\relax
     \def\@tempa{\phantom}\let\maybe@hspace\@gobble%
   \else%
     \def\@tempa{\vphantom}\let\maybe@hspace\hspace%
   \fi%
   \color{#1}\underline{\@tempa{#3}\maybe@hspace{#2}}}}
\makeatother

\begin{document}

$x$: \newunderline{1ex}{$x$} \par
$\frac{x}{y}$: \newunderline[red]{2em}{$\frac{x}{y}$} \par
$\dfrac{\mathrm{d}y}{\mathrm{d}x}$: \newunderline[blue!80!white]{!}{$\dfrac{\mathrm{d}y}{\mathrm{d}x}$}

\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​

\newunderline[<colour>]{<width>}{<stuff>} takes 3 arguments. The width is set to the natural width of <stuff> if its supplied as !. According to your request, it respects the depth of the argument, the mode (text or math) and can be of specified width (or natural).

share|improve this answer
    
Nice solution :-) –  azetina Sep 21 '12 at 19:27
add comment

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.