# \Underbar changing the style of font but \bar not, why?

The x alphabets should be of the same size and style. You can see below an ugly change in the style of x. How can I keep the font profile such as the size and style the same?

Ugly example where the size changes with \frac{\bar x_i+\underbar x_i}{2}.

Code

\documentclass[a4paper,english,titlepage,12pt]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}     %For theorems
\usepackage{amssymb}    %For things, \mathbb R.
\usepackage{bm}         %For bolding with greek letters

\begin{document}

\begin{align}
l_i &= \bar x_i-\underbar x_i \\
\hat x_i &= 0.5 (\bar x_i+\underbar x_i)
\end{align}

\end{document}

-
\underbar switches to text mode since it boxes its contents. You can use \underline instead. – Gonzalo Medina Mar 2 '14 at 16:40
\bar is declared as a maths accent by LaTeX whereas \underbar is a regular text command. fontmath.ltx contains \DeclareMathAccent{\bar}{\mathalpha}{operators}{"16} whereas latex.ltx includes \def\underbar#1{\underline{\sbox\tw@{#1}\dp\tw@\z@\box\tw@}}. – cfr Mar 2 '14 at 16:45

You are making it harder than it has to be. \underbar{$x$} resolves it without further packages or new commands. To get equal length use \overline{x} instead of \bar{x}. The bars are longer than in the above, but equal.

-
Welcome to TeX.SX! Could you please provide an example similarly to what Gonzalo and egreg did? This would help a lot future readers in understanding the validity of your approach. – Claudio Fiandrino Jan 22 at 14:18

The difference is that \underbar switches to text mode when it boxes its contents. The definition in the kernel is

\def\underbar#1{\underline{\sbox\tw@{#1}\dp\tw@\z@\box\tw@}}


On the other side, \bar is a math accent:

\DeclareMathAccent{\bar}{\mathalpha}{operators}{"16}


You can define a variation of the kernel's \underbar that boxes the contents in math mode:

\documentclass[a4paper,english,titlepage,12pt]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}     %For theorems
\usepackage{amssymb}    %For things, \mathbb R.
\usepackage{bm}         %For bolding with greek letters

\makeatletter
\def\munderbar#1{\underline{\sbox\tw@{$#1$}\dp\tw@\z@\box\tw@}}
\makeatother

\begin{document}
\newbox\mybox
\begin{align}
l_i &= \bar{x}_i-\munderbar{x}_i \\
\hat x_i &= 0.5 (\bar{x}_i+\munderbar{x}_i)
\end{align}

\end{document}


However, as you can see in the image the result is not very good. A better alternative is to use the accents package and its \underaccent command:

\documentclass[a4paper,english,titlepage,12pt]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}     %For theorems
\usepackage{amssymb}    %For things, \mathbb R.
\usepackage{bm}         %For bolding with greek letters
\usepackage{accents}

\newcommand\munderbar[1]{%
\underaccent{\bar}{#1}}

\begin{document}
\newbox\mybox
\begin{align}
l_i &= \bar{x}_i-\munderbar{x}_i \\
\hat x_i &= 0.5 (\bar{x}_i+\munderbar{x}_i)
\end{align}

\end{document}


-
I chose to use underline because not wanting to import too much packages even though it looks a bit nicer. +1 and thank you! – hhh Mar 2 '14 at 17:43

You have two choices: either use \underline or the accents package and its \underaccent feature; I'll show both.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{accents}

\newcommand{\ubar}[1]{\underaccent{\bar}{#1}}

\begin{document}

\begin{align*}
l_i &= \bar{x}_i-\underline{x}_i \\
\hat{x}_i &= 0.5 (\bar{x}_i+\underline{x}_i)
\end{align*}

\begin{align*}
l_i &= \bar{x}_i-\ubar{x}_i \\
\hat{x}_i &= 0.5 (\bar{x}_i+\ubar{x}_i)
\end{align*}

\end{document}


Avoid \bar x or \hat x; with braces it may seem more difficult to type, but it adds to clarity.

-
Why do I need \usepackage{accents} when underline works without it? – hhh Mar 2 '14 at 17:39
@hhh accents is necessary for the second example – egreg Mar 2 '14 at 17:42