Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
3  
\underbar switches to text mode since it boxes its contents. You can use \underline instead. –  Gonzalo Medina Mar 2 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 at 16:45

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
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 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}

enter image description here


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

share|improve this answer
    
Why do I need \usepackage{accents} when underline works without it? –  hhh Mar 2 at 17:39
1  
@hhh accents is necessary for the second example –  egreg Mar 2 at 17:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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