Math & ScienceCenter line not centered in cases environment

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

Center line not centered in cases environment

Post by Cham »

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 :

Code: Select all

\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
cases.jpg (22.22 KiB) Viewed 8441 times
I want the center line to be properly aligned with the "=". How to solve this ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Center line not centered in cases environment

Post by Stefan Kottwitz »

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:

Code: Select all

\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:

Code: Select all

\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
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Center line not centered in cases environment

Post by Cham »

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 ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Center line not centered in cases environment

Post by Cham »

Arrgh ! Your second solution doesn't work in the mdframed environment :

Code: Select all

\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}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Center line not centered in cases environment

Post by Stefan Kottwitz »

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
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Center line not centered in cases environment

Post by Stefan Kottwitz »

In the mdframed code you just have an additional \\ that should be removed, before the \vcenter.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Center line not centered in cases environment

Post by Cham »

Ah, yes, it's working great now. Thanks again !
Post Reply