14

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?

enter image description here

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

enter image description here

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}
  • 5
    \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
  • $$\underbar$$ why doesn't it work here? is it possible to put it in math exchange somehow? – Charlie Parker Sep 27 '18 at 0:11
2

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.

  • 1
    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 '16 at 14:18
  • $$\underbar$$ why doesn't it work here? is it possible to put it in math exchange somehow? – Charlie Parker Sep 27 '18 at 0:12
21

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}

enter image description here

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}

enter image description here

  • 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
  • $$\underbar$$ why doesn't it work here? is it possible to put it in math exchange somehow? – Charlie Parker Sep 27 '18 at 0:12
11

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}

enter image description here


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
  • 1
    @hhh accents is necessary for the second example – egreg Mar 2 '14 at 17:42
  • $$\underbar$$ why doesn't it work here? is it possible to put it in math exchange somehow? – Charlie Parker Sep 27 '18 at 0:12
  • @CharlieParker \underbar is a text command. – egreg Sep 27 '18 at 8:27
  • @egreg what are the consequences of that? – Charlie Parker Sep 27 '18 at 18: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.