How to keep a nice layout when combining overline/underline with superscript/subscript?

I would like to add indices as subscripts and superscripts to a couple of bounds denoted by underline (lower bound) and overline (upper bound), but I am not able to keep the same well-shaped look of the overall symbol, since the superscript is raised and the subscript is lowered with respect to the symbol not underlined/overlined (so that the resulting formula exceeds the textstyle space). Am I doing something wrong?

PS: The same problem affects only the subscript when used together with a superscript:

$\theta_k \; \underline \theta_k$
$\theta^{\delta} \; \overline \theta^{\delta}$
$\theta_k \; \theta^{\delta} \; \theta_k^{\delta}$


This still doesn't take care of line 3 (combined sub/superscript), but it does remedy the under/overline issue. I personally do not find the line 3 issue objectionable.

\documentclass{article}
\newcommand\utheta{\vphantom{theta}\smash{\underline \theta}}
\newcommand\otheta{\vphantom{theta}\smash{\overline \theta}}
\begin{document}
$\theta_k \; \utheta_k$
$\theta^{\delta} \; \otheta^{\delta}$
$\theta_k \; \theta^{\delta} \; \theta_k^{\delta}$
\end{document}


• thank you, this works perfectly. As regards the 'third line', I am aware that Latex has to process the whole formula in order to get a cleaner output; what puzzles me is the fact that, at least in principle, there is enough space for both subscripts and superscripts, so the overall layout is a little bit odd or too stretched out (maybe this is due to the skew positioning of the theta character). Can I ask you which is the combined role of \vphantom and \smash in your solution? Dec 31 '16 at 16:16
• @Vexx23 The \smash says to treat its argument as if it had zero height, effectively removing the \overline and \underline from affecting the script placement. The \vphantom says to insert a zero-width nothing, but which occupies the same vertical footprint as its argument. Thus, a nothing the size of a \theta is placed, followed by something which is treated as zero height, but has the desired appearance of a barred-theta. Dec 31 '16 at 17:13

In my opinion, the \underaccent{\bar} command, or \underaccent{\widebar}, borrowing the \widebar symbol from mathabx, looks better, because it takes into account the italic angle of the glyphs:

\documentclass{article}
\usepackage{amsmath, accents}%

\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{
<5><6><7><8><9><10>
<10.95><12><14.4><17.28><20.74><24.88>
mathx10
}{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareFontSubstitution{U}{mathx}{m}{n}
\DeclareMathAccent{\widebar}{0}{mathx}{"73}

\newcommand\utheta{\vphantom{theta}\smash{\underline \theta}}
\newcommand\otheta{\vphantom{theta}\smash{\overline \theta}}

\begin{document}

$\theta_k \; \utheta_k \;\underaccent{\bar}{\theta}_k\;\underaccent{\widebar}{\theta}_k$
$\theta^{\delta} \; \otheta^{\delta} \;\bar{\theta}^{\delta} \;\widebar{\theta}^{\delta}$

\end{document}


• third and fourth outputs are by far cleaner, it is something one realizes only when seen up close. Jan 2 '17 at 11:12