LaTeX forum ⇒ Math & ScienceCenter line not centered in cases environment Topic is solved

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

Center line not centered in cases environment

Postby Cham » Sun Jul 16, 2017 4:41 pm

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 :

  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage{lmodern}
  3. \usepackage{amsmath}
  4. \usepackage{tensor}
  5. \usepackage{pifont}
  6.  
  7. \begin{document}
  8.  
  9. \begin{equation*}
  10. \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =
  11. \begin{cases}
  12. 0, &\text{if $x < -\, \varepsilon$.} \\[8pt]
  13. \displaystyle{\frac{1}{2 \varepsilon}}, &\text{if $-\, \varepsilon < x < \varepsilon$.} \\[8pt]
  14. 0, &\text{if $x > \varepsilon$.}
  15. \end{cases}
  16. \end{equation*}
  17.  
  18. \end{document}


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


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 8253
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Jul 16, 2017 5:06 pm

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:

  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage{lmodern}
  3. \usepackage{amsmath}
  4. \usepackage{tensor}
  5. \usepackage{pifont}
  6. \makeatletter
  7. \renewcommand*\env@cases[1][1.2]{%
  8. \let\@ifnextchar\new@ifnextchar
  9. \left\lbrace
  10. \def\arraystretch{#1}%
  11. \array{@{}l@{\quad}l@{}}%
  12. }
  13. \makeatother
  14. \begin{document}
  15. \begin{equation*}
  16. \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =
  17. \begin{cases}[2]
  18. 0, &\text{if $x < -\, \varepsilon$.} \\%[8pt]
  19. \dfrac{1}{2 \varepsilon\strut},
  20. &\text{if $-\, \varepsilon < x < \varepsilon$.} \\%[8pt]
  21. 0, &\text{if $x > \varepsilon$.}
  22. \end{cases}
  23. \end{equation*}
  24. \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:

  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage{lmodern}
  3. \usepackage{amsmath}
  4. \usepackage{tensor}
  5. \usepackage{pifont}
  6. \begin{document}
  7. \begin{equation*}
  8. \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =
  9. \begin{cases}
  10. 0, &\text{if $x < -\, \varepsilon$.} \\
  11. \dfrac{1}{2 \varepsilon\strut},
  12. &\text{if $-\, \varepsilon < x < \varepsilon$.}
  13. \vcenter{\rule{0pt}{8ex}}\\
  14. 0, &\text{if $x > \varepsilon$.}
  15. \end{cases}
  16. \end{equation*}
  17. \end{document}


Stefan
Site admin

User avatar
Cham
Posts: 680
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sun Jul 16, 2017 5:20 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 ?

User avatar
Cham
Posts: 680
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sun Jul 16, 2017 5:35 pm

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

  1. \documentclass[12pt,oneside]{article}
  2. \usepackage[letterpaper,margin=1in]{geometry}
  3. \usepackage{lmodern}
  4. \usepackage{amsmath}
  5. \usepackage{amssymb}
  6. \usepackage{amsfonts}
  7. \usepackage{mathtools}
  8. \usepackage{tensor}
  9. \usepackage{xcolor}
  10. \usepackage{mdframed}
  11. \begin{document}
  12.  
  13. \begin{mdframed}[linecolor=gray,linewidth=1pt,leftmargin=0cm,rightmargin=0cm,topline=true,bottomline=true,innertopmargin=6pt,shadow=true,shadowsize=2pt,shadowcolor=gray]
  14.  
  15. \begin{equation*}
  16. \delta(x) \; \Rightarrow \; \tensor{\delta}{_{\varepsilon}}(x) =
  17. \begin{cases}
  18. 0, &\text{si $x < -\, \varepsilon$.} \\
  19. \dfrac{1}{2 \varepsilon}, &\text{si $-\, \varepsilon < x < \varepsilon$.} \\
  20. \vcenter{\rule{0pt}{8ex}} \\
  21. 0, &\text{si $x > \varepsilon$.}
  22. \end{cases}
  23. \end{equation*}
  24.  
  25. \end{mdframed}
  26.  
  27. \end{document}

User avatar
Stefan Kottwitz
Site Admin
Posts: 8253
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Jul 16, 2017 5:36 pm

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

User avatar
Stefan Kottwitz
Site Admin
Posts: 8253
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Jul 16, 2017 5:38 pm

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

Stefan
Site admin

User avatar
Cham
Posts: 680
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sun Jul 16, 2017 5:42 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 1 guest