21

The highlighted part has the equation centred, which looks rather imbalance with the top equation, is it possible to align the highlighted part to the left? Below is the code I am using.

\begin{equation}~\label{eq:prob1}
p_k(s)=
\begin{cases}
    \displaystyle\frac{c_k-q_k(s)}{c_k- \overline{c_k}} & if\ q_k(s) >  \overline{c_k} \\
    1                                                   & if\ q_k(s) \leq  \overline{c_k}
\end{cases}
\end{equation}

enter image description here

1
  • 6
    It doesn't look unbalanced to me.
    – egreg
    Nov 25, 2012 at 22:38

6 Answers 6

25

You can use

\frac{c_k-q_k(s)}{c_k- \overline{c_k}\hfill}

to force the denominator to the left

16

The \hfill solution works in this particular case only because identical terms are used as the first term for both the denominator and numerator. For more general cases, this may not quite provide the desired alignment. For example, if the numerator contained w_k and the denominator c_k, using just \hfill would provide the alignment as illustrated by the last example below.

This solution provides an \AlignMe macro that has one optional parameter and two mandatory parameters:

\AlignMe[<alignment>]{<content to determine space>}{<content to be typeset>}

where <alignment> can be the usual single letters of left, center, and right.

Below are a few examples illustrating this and comparing to the \hfill solution where the alignment of the first term is adjusted and the alignment of the minus sign remains intact:

enter image description here

Notes:

  • You should use \text{if} instead of if as Hendrik Vogt commented.
  • My personal preference would be equation (1). Even though that results in the denominator not being centered with respect to the vinculum being

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{calc}

% Add color here to simplify comparrison of the alignment
\newcommand{\CK}{\textcolor{red}{c_k}}
\newcommand{\CkBar}{\textcolor{blue}{\overline{c_k}}}

\newcommand*{\AlignMe}[3][c]{%
\makebox[\widthof{$#2$}][#1]{$#3$}}%
\begin{document}\noindent
Using \verb|\AlignMe[r]| for first and \verb|\AlignMe[l]| for second term:
\begin{equation}
p_k(s)=
\begin{cases}
    \dfrac{w_k -q_k(s)}{\AlignMe[r]{w_k}{\CK} - \AlignMe[l]{q_k(s)}{\CkBar}} 
            & \text{if } q_k(s)   >   \overline{c_k} \\
       1    & \text{if } q_k(s) \leq  \overline{c_k}
\end{cases}
\end{equation}
%
Using \verb|\AlignMe[r]| and \verb|\hfill|:
\begin{equation}
p_k(s)=
\begin{cases}
    \dfrac{w_k -q_k(s)}{\AlignMe[r]{w_k}{\CK} - \CkBar\hfill} 
            & \text{if } q_k(s)   >   \overline{c_k} \\
       1    & \text{if } q_k(s) \leq  \overline{c_k}
\end{cases}
\end{equation}
%
Using \verb|\AlignMe[r]| for first and \verb|\AlignMe[c]| for second term:
\begin{equation}
p_k(s)=
\begin{cases}
    \dfrac{w_k -q_k(s)}{\AlignMe[r]{w_k}{\CK} - \AlignMe[c]{q_k(s)}{\CkBar}} 
            & \text{if } q_k(s)   >   \overline{c_k} \\
       1    & \text{if } q_k(s) \leq  \overline{c_k}
\end{cases}
\end{equation}
%
For comparrison, using \verb|\hfill|:
\begin{equation}
p_k(s)=
\begin{cases}
    \dfrac{w_k -q_k(s)}{\CK - \CkBar\hfill} 
            & \text{if } q_k(s)   >   \overline{c_k} \\
       1    & \text{if } q_k(s) \leq  \overline{c_k}
\end{cases}
\end{equation}
\end{document}
0
9

The fraction doesn't look unbalanced to me. However, some small refinements might improve its appearance.

The main problem I see is in the overline, which sticks too much to the right. Here are six proposals (the twocolumn option is just for getting a reduced text width)

\documentclass[twocolumn]{article}
\usepackage{amsmath}
\begin{document}
\begin{gather}
\frac{c_k-q_k(s)}{c_k- \overline{c_k}}\\
\frac{c_k-q_k(s)}{c_k- \overline{c_k}\,}\\
\frac{c_k-q_k(s)}{c_k- \overline{c_k\mkern-2mu}\mkern2mu}\\
\frac{c_k-q_k(s)}{c_k- \overline{c_k\mkern-2mu}\mkern2mu\,}\\
\frac{c_k-q_k(s)}{c_k- \mkern1.5mu\overline{\mkern-1.5mu c_k\mkern-2mu}\mkern2mu}\\
\frac{c_k-q_k(s)}{c_k- \mkern1.5mu\overline{\mkern-1.5mu c_k\mkern-2mu}\mkern2mu\,}
\end{gather}
\end{document}

The trick for the \mkern can of course be hidden in a macro; the \, at the end of the second and fourth examples pushes the denominator a bit to the left. In the fifth and sixth examples, the bar overhang of the bar is reduced also at the left.

Pushing the denominator all the way to the left is, in my opinion, wrong.

enter image description here

0
5

You also use \phantom{} to create whitespace of desired character width. In this case \phantom{(s)},

\dfrac{c_k-q_k(s)}{c_k- \overline{c_k}\phantom{(s)}}

Also I replaced \displaystyle\frac by \dfrac instead.

1

For those of us who mostly use horizontal aligment for fractions as a whole, the (less general) \fracAlignTop and \fracAlignBottom macros might come in handy:

% Numerator alignment
\newcommand{\fracAlignTop}[3][l]{%          align the numerator   according to the width of the denominator
    \frac{%
        \AlignMe[#1]{#3}{#2}%
    }{#3}}%

% Denominator alignment
\newcommand{\fracAlignBottom}[3][l]{%       align the denominator according to the width of the numerator
    \frac{%
        #2}{%
        \AlignMe[#1]{#2}{#3}%
    }}%

... where the optional argument accepts "the usual single letters of left, center, and right." Thus, for example:

\fracAlignBottom[l]{c_k-q_k(s)}{c_k- \overline{c_k}}

produces

\fracAlignBottom[l]{c_k-q_k(s)}{c_k- \overline{c_k}}

Mind that this builds on top of the \AlignMe macro by @Peter Grill (kudos, mate). Nevertheless, my solution answers the original question and is, IMHO, useful because it is less prone to input associated errors (i.e. \fracAlignTop has syntax similar to the original \frac).

1

Here's my "rough but quick" solution to the problem. (The purist LaTeXer might prefer the safer solution of using package letltxmacro as described by @egreg in 47351.)

\documentclass{article}

\let\oldfrac\frac
\renewcommand{\frac}[3][]{\ifx|#1|\oldfrac{#2}{#3}\else\begin{array}{#1}{#2}\\\hline{#3}\end{array}\fi}
\begin{document}
\[
  1+r+r^2+\cdots+r^n
  =
  \frac{1-r^{n+1}}{1-r}
  =
  \frac[l]{1-r^{n+1}}{1-r}
  =
  \frac[r]{1-r^{n+1}}{1-r}
  =
  \frac[c]{1-r^{n+1}}{1-r}    
\]
\end{document}

resulting in enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .