TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


This works just fine but the following does not:


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
@azetina: Wait, are you saying that there might be an alternative way -- other than \tikzmark? :-) – Peter Grill Sep 21 '12 at 3:51
up vote 6 down vote accepted

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

enter image description here

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\newunderline}[3][black]{% \newunderline[<colour>]{<width>}{<stuff>}


$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}$}


\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

Your Answer


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.