## LaTeX forum ⇒ Math & Science ⇒ Center line not centered in cases environment Topic is solved

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Cham
Posts: 830
Joined: Sat Apr 02, 2011 4:06 pm

### Center line not centered in cases environment

I'm getting a small glitch in a cases environment. As you should see with this MWE, the middle line is not exactly aligned with the equal sign on the left, and the vertical spacing isn't pretty :

\documentclass[12pt,letterpaper,twoside]{book}\usepackage{lmodern}\usepackage{amsmath}\usepackage{tensor}\usepackage{pifont} \begin{document} 		\begin{equation*}		\delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =		\begin{cases}		0, &\text{if $x < -\, \varepsilon$.} \\[8pt]		\displaystyle{\frac{1}{2 \varepsilon}}, &\text{if $-\, \varepsilon < x < \varepsilon$.} \\[8pt]		0, &\text{if $x > \varepsilon$.}		\end{cases}	\end{equation*} \end{document}

Here's a preview :
cases.jpg (22.22 KiB) Viewed 1007 times

I want the center line to be properly aligned with the "=". How to solve this ?

Stefan Kottwitz
Site Admin
Posts: 8704
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
Hi Cham,

very good minimal example!

That glitch only occurs because of the manually inserted space of 8pt. The first line is made higher, the second line is made higher, the last one isn't. Somehow we got an asymmetry. If you omit this, it's still perfectly centered.

We can stretch it in another way. One way is redefining cases in order to get it stretched, such as by \arraystretch. I did this in my blog some time ago. This introduces an optional argument for stretching:

\documentclass[12pt,letterpaper,twoside]{book}\usepackage{lmodern}\usepackage{amsmath}\usepackage{tensor}\usepackage{pifont}\makeatletter\renewcommand*\env@cases[1][1.2]{%  \let\@ifnextchar\new@ifnextchar  \left\lbrace  \def\arraystretch{#1}%  \array{@{}l@{\quad}l@{}}%}\makeatother\begin{document}\begin{equation*}  \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =  \begin{cases}[2]    0, &\text{if $x < -\, \varepsilon$.} \\%[8pt]    \dfrac{1}{2 \varepsilon\strut},        &\text{if $-\, \varepsilon < x < \varepsilon$.} \\%[8pt]    0, &\text{if $x > \varepsilon$.}  \end{cases}\end{equation*}\end{document}

You may notice that all is stretched, so the brace is higher too.

Another way is adding an invisible line (0pt wide) that is vertically centered, in the middle line. So \vcenter{\rule{0pt}{8ex}} it preserves the centering:

\documentclass[12pt,letterpaper,twoside]{book}\usepackage{lmodern}\usepackage{amsmath}\usepackage{tensor}\usepackage{pifont}\begin{document}\begin{equation*}  \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =  \begin{cases}    0, &\text{if $x < -\, \varepsilon$.} \\    \dfrac{1}{2 \varepsilon\strut},        &\text{if $-\, \varepsilon < x < \varepsilon$.}        \vcenter{\rule{0pt}{8ex}}\\    0, &\text{if $x > \varepsilon$.}  \end{cases}\end{equation*}\end{document}

Stefan
Site admin

Cham
Posts: 830
Joined: Sat Apr 02, 2011 4:06 pm
Thanks a lot Stefan !

I'm studying the second (simpler) method, since I don't want to make my preamble more complicated as it is already !

EDIT : Why the [cmd]\strut[/cmd] command ?

Cham
Posts: 830
Joined: Sat Apr 02, 2011 4:06 pm
Arrgh ! Your second solution doesn't work in the mdframed environment :

\documentclass[12pt,oneside]{article}\usepackage[letterpaper,margin=1in]{geometry}\usepackage{lmodern}\usepackage{amsmath}\usepackage{amssymb}\usepackage{amsfonts}\usepackage{mathtools}\usepackage{tensor}\usepackage{xcolor}\usepackage{mdframed}\begin{document} \begin{mdframed}[linecolor=gray,linewidth=1pt,leftmargin=0cm,rightmargin=0cm,topline=true,bottomline=true,innertopmargin=6pt,shadow=true,shadowsize=2pt,shadowcolor=gray] 	\begin{equation*}		\delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =		\begin{cases}			0, &\text{si $x < -\, \varepsilon$.} \\			\dfrac{1}{2 \varepsilon}, &\text{si $-\, \varepsilon < x < \varepsilon$.} \\			\vcenter{\rule{0pt}{8ex}} \\			0, &\text{si $x > \varepsilon$.}		\end{cases}	\end{equation*} \end{mdframed} \end{document}

Stefan Kottwitz
Site Admin
Posts: 8704
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
At first I inserted the \strut (an invisible line over the line height) because the fraction line seemed to be a bit too close to the lower part, I think. It can be omitted.

Stefan
Site admin

Stefan Kottwitz
Site Admin
Posts: 8704
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
In the mdframed code you just have an additional \\ that should be removed, before the \vcenter.

Stefan
Site admin

Cham
Posts: 830
Joined: Sat Apr 02, 2011 4:06 pm
Ah, yes, it's working great now. Thanks again !

Return to “Math & Science”

### Who is online

Users browsing this forum: No registered users and 4 guests