2

How can I add a vertical spacing between the underline and other items by adjusting the \colorunderline command?

\documentclass[12pt,b5paper]{article}
\usepackage{amsmath}
\usepackage{color}

\newsavebox\UnderlineBox
\newcommand\colorunderline[2][red]{%
        {\sbox\UnderlineBox{$#2$}%
        \rlap{\usebox\UnderlineBox}\color{#1}%
        \raisebox{-5pt}{\mbox{\rule[-\dp\UnderlineBox]{\wd\UnderlineBox}{3pt}}}%
        }%
}

\begin{document}


\begin{align}
\begin{array}{c}
A\\
\colorunderline[red]{A}\\
A
\end{array}
\end{align}

\end{document}

enter image description here

  • 2
    It's not very clear in what context you're using this; the space below can be adjusted using \\[5pt] (or similar). – egreg Dec 6 '15 at 23:25
  • I want that the underline has some defined invisible border below the line which I can adjust? How can I do this – Gabriel Dec 7 '15 at 11:53
1

Here's an \innercolorunderline macro that does what you need, the list of arguments should be self-explanatory. I added a handier key-value interface with some examples.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\newcommand{\innercolorunderline}[4]{%
  % #1 = space above rule, #2 = space below rule, #3 = color, #4 = symbol
  \vtop{
    \offinterlineskip
    \sbox0{$#4$}
    \copy0
    \kern#1
    \hbox{\color{#3}\vrule height 3pt width \wd0}
    \kern#2
  }%
}

\ExplSyntaxOn
\keys_define:nn { gabriel/colorunderline }
 {
  color .tl_set:N  = \l_gabriel_colorunderline_color_tl,
  color .initial:n = red,
  above .dim_set:N = \l_gabriel_colorunderline_above_dim,
  above .initial:n = 2pt,
  below .dim_set:N = \l_gabriel_colorunderline_below_dim,
  below .initial:n = 0pt,
 }

\NewDocumentCommand{\colorunderline}{O{}m}
 {
  \group_begin:
  \keys_set:nn { gabriel/colorunderline } { #1 }
  \gabriel_colorunderline:VVVn
    \l_gabriel_colorunderline_above_dim
    \l_gabriel_colorunderline_below_dim
    \l_gabriel_colorunderline_color_tl
    { #2 }
  \group_end:
 }
\cs_set_eq:NN \gabriel_colorunderline:nnnn \innercolorunderline
\cs_generate_variant:Nn \gabriel_colorunderline:nnnn { VVV }
\ExplSyntaxOff

\begin{document}

\[
\begin{array}[t]{c}
A\\
\colorunderline{A}\\
A
\end{array}
\quad
\begin{array}[t]{c}
A\\
\colorunderline[color=green]{A}\\
A
\end{array}
\quad
\begin{array}[t]{c}
A\\
\colorunderline[above=6pt]{A}\\
A
\end{array}
\quad
\begin{array}[t]{c}
A\\
\colorunderline[color=red,above=6pt,below=6pt]{A}\\
A
\end{array}
\]

\end{document}

enter image description here

  • favbulous! even if I dont understand the code – Gabriel Dec 7 '15 at 15:24
  • @Gabriel The first macro just stacks two boxes, taking as reference point the top one. Some customizable space is inserted between the boxes and at the bottom. The rest is just defining a key-value interface. – egreg Dec 7 '15 at 15:32
  • why does that not work in inline math? – Gabriel Dec 7 '15 at 15:38
  • the spacing seems to be wrong in inline math? – Gabriel Dec 7 '15 at 15:40
  • @Gabriel I forgot \offinterlineskip! Fixed. – egreg Dec 7 '15 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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