5

I want to use \underline for tensors (please don't question) but, for example, I don't like how $\underline{K}$ looks. Why?

\documentclass{article}
\begin{document}
    $$\underline{K}~\underline{\mathrm{K}}$$
\end{document}

Very simply, I would expect that the "base" of the character be underlined, not the whole character. That is, get rid of the red part of the underlining. In other words, I would like to underline first and then italicize, instead of italicizing first and then underlining. Can this be achieved? \mathit{\underline{\mathrm{K}}} did not work, of course.

Edit: Since I want to stay in math mode, here's another detail: let's make it work for $\mathit{\Pi}$.

K and K

bers
  • 5,404
  • 2
    Don't use $$...$$. \underline uses the full with of the character box which is wider for italics than for upright shape –  Oct 15 '15 at 00:48
  • 1
    Underlining and italicising are different kinds of animal. When you use italic, you use characters from a different font. It isn't a question of the upright character being transformed. It is a different character from a different font. When you underline, that is an addition to what is there, based on the size of the character. TeX doesn't know anything about the 'base' of the character. All it knows is how big the box is. In the case of an italic K, the box is wider than in the case of an upright one. soul works for text, taking account of italic correction, I assume, but not \ul{$K$}. – cfr Oct 15 '15 at 00:58
  • @ChristianHupfer: I usually don't use $$, but thanks! @cfr: Thanks! I like how you spend five lines on explaining why it cannot work, just to present a solution in the last line :) [which does not work in math mode, but does in text mode]. – bers Oct 15 '15 at 01:28
  • Yes, compare \underline{\textit{K}}~\ul{\textit{K}}. soul's \ul does provide an advantage here in terms of italic correction (even though I don't like the distance of that underlining). – bers Oct 15 '15 at 01:44
  • What I meant was: you can't do it by underlining and then italicising per your suggestion because the characters for italic are not ever upright to be underlined. I don't know how soul works but either it approximates using the non-italic font or it somehow else corrects for the italic. What it definitely is not doing is underline the text and then make that text italic. – cfr Oct 15 '15 at 02:07
  • I EDITED my answer to work with \mathit{\Pi}. – Steven B. Segletes Oct 15 '15 at 02:08

2 Answers2

6

Here, I stack an underline of a phantom mathrm K atop the math K, keeping the stack left aligned. I compare with the original.

\documentclass{article}
\usepackage{stackengine}
\begin{document}
\[
\def\stacktype{L}\def\stackalignment{l}
\stackon[0pt]{$K$}{$\underline{\phantom{\mathrm{K}}}$}
\quad\underline{K}
\]
\end{document}

enter image description here

To turn it into a macro, \baseunderline{} that works in different math styles... EDITED to work with \mathit{\Pi}.

\documentclass{article}
\usepackage{stackengine,scalerel}
\def\baseunderline#1{\def\stacktype{L}\def\stackalignment{l}%
  \ThisStyle{\stackon[0pt]{$\SavedStyle#1$}{\let\mathit\relax%
  $\SavedStyle\underline{\phantom{\mathrm{#1}}}$}}}
\begin{document}
\[\baseunderline{K}\quad\underline{K}\]
\[\scriptstyle\baseunderline{K}\quad\underline{K}\]
\[\scriptscriptstyle\baseunderline{K}\quad\underline{K}\]
\[  \baseunderline{\mathit{\Pi}}\]
\end{document}

enter image description here

Bers points out that for certain applications, defining the stacktype and alignment should be done inside the \ThisStyle invocation. To that end, one may actually specify all stacking parameters at the time of invocation, with the following definition:

\def\baseunderline#1{%
  \ThisStyle{\stackengine{0pt}{$\SavedStyle#1$}{\let\mathit\relax%
  $\SavedStyle\underline{\phantom{\mathrm{#1}}}$}{O}{l}{F}{F}{L}}}
  • Looks very nice! – bers Oct 15 '15 at 02:10
  • 1
    In some applications (e.g., with \bm), I found out it may be helpful to have \def\stacktype{L} \def\stackalignment{l} within the body of \ThisStyle. – bers Oct 15 '15 at 03:26
  • 1
    @bers Perhaps one should just cut to the chase and define it this way: \def\baseunderline#1{% \ThisStyle{\stackengine{0pt}{$\SavedStyle#1$}{\let\mathit\relax% $\SavedStyle\underline{\phantom{\mathrm{#1}}}$}{O}{l}{F}{F}{L}}}, which specifies the parameters directly in the invocation. I'll note it in the answer. – Steven B. Segletes Oct 15 '15 at 10:17
2

Combining \underbar with \itshape in math mode seems to do the trick. Not sure how robust this is though.

\documentclass{article}
\newcommand{\mytensor}[1]{\underbar{\itshape #1}}
\begin{document}
$K$
$\underbar{K}$
$\mytensor{K}$
\end{document}

enter image description here

erik
  • 12,673
  • Playing around I bit, I have found $\underline{\textit{K}}$ which also works. Now, which one is better? Both seem to leave math mode (\textit, obviously; \underbar, judging from $\underbar{\mathit{K}}$ failing... – bers Oct 15 '15 at 01:52
  • @bers Since you're using this notation for a tensor, presumably in math mode, I thought using text-mode commands should be avoided. – erik Oct 15 '15 at 01:56
  • 1
    I agree. \underbar sets its contents in text mode, though: https://tex.stackexchange.com/questions/163280/underbar-changing-the-style-of-font-but-bar-not-why – bers Oct 15 '15 at 01:58
  • That explains why \underbar{K} didn't work as I had expected. Perhaps adding \ensuremath would help (see edit). – erik Oct 15 '15 at 02:04
  • $\mytensor{K}$'s underscore is too long; \Gamma is not italic. I am not so positive \underbar is the way to go. – bers Oct 15 '15 at 02:08
  • I hadn't noticed the underbar got longer when I added \ensuremath. \Gamma was just in there to make sure it worked with a math argument. – erik Oct 15 '15 at 02:11