# What is the macro used to define \underline{}?

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

-
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

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

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

-
Nice solution :-) –  azetina Sep 21 '12 at 19:27
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9208378195762634, "perplexity": 2845.1634037869235}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-40/segments/1443736677221.11/warc/CC-MAIN-20151001215757-00124-ip-10-137-6-227.ec2.internal.warc.gz"}