4

I try to put an equation on table, but it doesn't fit right. Specifically, the fit is much too tight.

\begin{longtable}{|c|c|}
\hline 
• & $\begin{array}{ll}
\Rightarrow a+b &> \dfrac{2013}{b}(a+b)+\dfrac{2014}{a}(a+b) \\
    &= 2013 + \dfrac{2013a}{b}+\dfrac{2014b}{a} + 2014 \\
    &\geqslant 2013 + 2\sqrt{\dfrac{2013a}{b}.\dfrac{2014b}{a}} + 2014 \\
    &= \left( \sqrt{2013} + \sqrt{2014} \right)^2
\end{array}$ \\ 
\hline 
• & • \\ 
\hline 
\end{longtable} 

enter image description here

I'm working on a long table, and I got amount of situations like this. How can I fix it ?

  • Welcome to TeX.SE! Please post not just code snippets but a fully compilable minimal example. That way, would-be writers of answers don't have to guess at which document class you use and which packages must be loaded in order to get your code to compile. :-) – Mico Apr 2 '17 at 8:16
5

Some suggestions:

  • Most importantly, don't use an array environment, which uses textstyle-math settings by default. Use an aligned environment, as its default is displaystyle math.

  • Don't use a . (dot) as a symbol to denote multiplication; use \cdot instead. Or, just omit the symbol as it's not really needed.

    Remark: If you wanted the \textbullet item in the first column to be aligned at the top rather than in the middle, simply use \begin{aligned}[t] instead of just \begin{aligned}.

  • Insert a (typographic) strut in one of the numerator terms in the first row; this will increase the vertical distance to the preceding horizontal line.

  • The parentheses generated by \left( and \right) in the final row are too large, typographically speaking. Instead, use \bigl( and \bigr).

  • Optional: load the array package and issue the directive \setlength\extrarowheight{2pt} to create a table with a (slightly) more "open" look.

enter image description here

\documentclass{article}
\usepackage{longtable,amsmath,amssymb}
\usepackage{array} % for \extrarowheight macro
\setlength\extrarowheight{2pt} % for a more "open" look
\newcommand\Tstrut{\smash[b]{\strut}} % "top strut"
\begin{document}
\begin{longtable}{|c|c|}
\hline 
\textbullet & 
  $\begin{aligned}
  \Rightarrow a+b &> \frac{2013\Tstrut}{b}(a+b)+\frac{2014}{a}(a+b) \\
    &= 2013 + \frac{2013a}{b}+\frac{2014b}{a} + 2014 \\
    &\geqslant 2013 + 2\sqrt{\frac{2013a}{b}\cdot\frac{2014b}{a}} + 2014 \\
    &= \bigl( \sqrt{2013} + \sqrt{2014} \,\bigr)^2
  \end{aligned}$ \\ 
\hline 
\textbullet & \textbullet \\ 
\hline 
\end{longtable} 
\end{document}


Addendum to address the OP's follow-up question about how one might automate (and improve) the spacing around horizontal lines. In my view, one is actually asking the wrong question if one wishes to improve the vertical spacing above and below horizontal lines drawn by \hline and \cline. Instead, one should ask, why are we using \hline and \cline at all? The booktabs package provides the macros \toprule, \midrule, \cmidrule, and \bottomrule, which automatically produce much better spacing. Using these macros instead of \hline and \cline, and omitting all vertical rules, immediately produces a much more pleasing and easy-to-grasp layout. This is illustrated in the following screenshot.

enter image description here

\documentclass{article}
\usepackage{longtable,amsmath,amssymb,booktabs}
\begin{document}
\begin{longtable}{@{}cc@{}}

\toprule 
\endhead
\bottomrule
\endfoot

\textbullet & 
  $\begin{aligned}[t]
  \Rightarrow a+b &> \frac{2013}{b}(a+b)+\frac{2014}{a}(a+b) \\
    &= 2013 + \frac{2013a}{b}+\frac{2014b}{a} + 2014 \\
    &\geqslant 2013 + 2\sqrt{\frac{2013a}{b}\cdot\frac{2014b}{a}} + 2014 \\
    &= \bigl( \sqrt{2013} + \sqrt{2014} \,\bigr)^2
  \end{aligned}$ \\ 

\midrule

\textbullet & \textbullet \\ 

\end{longtable} 
\end{document}
  • it actually works nicely, thank you. By the way, I have a lot of \dfrac in my table, so is there any idea to fix it, instead of adding \mathstrut to all ? – Nguyen Bao Apr 2 '17 at 9:25
  • @NguyenBao - A very good question! I think it's sufficiently important that I've written an addendum to my answer. Basically, I think the optimal solution to the spacing issues raised by the output of \dfrac being too close to the preceding \hline is to stop using \hline and \cline entirely. Instead, use the line-drawing commands of the booktabs package. And, while you're at it, omit all vertical rules: they aren't needed, and they won't be missed. Really! Give it a try. – Mico Apr 2 '17 at 10:01
  • Thank you for helping me so enthusiastic, but another problem is coming. Some rows I used \cdashline (by using rthe arydshln package) to seperate, now, the booktabs package draws lines automatically, so if I want to add a dash line, what should I do ? – Nguyen Bao Apr 3 '17 at 2:19
  • @NguyenBao - Instead of \cdashline (or \cline...), consider using \addlinespace -- just a band of whitespace. It's amazingly effective. – Mico Apr 3 '17 at 5:23
5

with cellspace and amsmath (mathtools) package:

enter image description here

\documentclass{article}
\usepackage{mathtools, amssymb}
\usepackage{cellspace, longtable}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}

\begin{document}
\begin{longtable}{|c|Sc|}% <-- S activate settings for cellspace
    \hline
\textbullet &
    $\begin{aligned}
\Rightarrow a+b & > \frac{2013}{b}(a+b)+\frac{2014}{a}(a+b) \\
                & = 2013 + \frac{2013a}{b}+\frac{2014b}{a} + 2014 \\
                & \geqslant 2013 + 2\sqrt{\frac{2013a}{b}\cdot\frac{2014b}{a}} + 2014 \\
                & = \bigl( \sqrt{2013} + \sqrt{2014} \,\bigr)^2
     \end{aligned}$             \\
    \hline
\textbullet     & \textbullet   \\
    \hline
\end{longtable}
\end{document}
  • Since you're loading the cellspace package and are setting the length variable cellspacetoplimit, it's no longer necessary to insert a \mathstrut, right? – Mico Apr 2 '17 at 9:31
  • @Mico, I overlooked it ... of course it is in my case surplus. Deleted – Zarko Apr 2 '17 at 10:49
2

You use display fractions dfrac in your array, so you need to increase \arraystretch to accommodate them. For example,

\documentclass{article}
\usepackage{longtable,amsmath,amssymb}
\usepackage[utf8]{inputenc}
\pagestyle{empty}
\begin{document}
\begin{longtable}{|c|c|}
\hline 
• & \renewcommand\arraystretch{2.5}$\begin{array}{ll}
\Rightarrow a+b &> \dfrac{2013}{b}(a+b)+\dfrac{2014}{a}(a+b) \\
    &= 2013 + \dfrac{2013a}{b}+\dfrac{2014b}{a} + 2014 \\
    &\geqslant 2013 + 2\sqrt{\dfrac{2013a}{b}.\dfrac{2014b}{a}} + 2014 \\
    &= \left( \sqrt{2013} + \sqrt{2014} \right)^2
\end{array}$ \\ 
\hline 
• & • \\ 
\hline 
\end{longtable} 
\end{document}

enter image description here

Update @Mico asked in comments to make a more consistent spacing around the >. Switching to aligned does this. And, while we at it, let's use booktabs too:

\documentclass{article}
\usepackage{longtable,amsmath,amssymb, booktabs}
\usepackage[utf8]{inputenc}
\pagestyle{empty}
\begin{document}
\begin{longtable}{cc}
\toprule
• &\renewcommand\arraystretch{2.5}$\begin{aligned}
\Rightarrow a+b &> \dfrac{2013}{b}(a+b)+\dfrac{2014}{a}(a+b) \\
    &= 2013 + \dfrac{2013a}{b}+\dfrac{2014b}{a} + 2014 \\
    &\geqslant 2013 + 2\sqrt{\dfrac{2013a}{b}.\dfrac{2014b}{a}} + 2014 \\
    &= \left( \sqrt{2013} + \sqrt{2014} \right)^2
\end{aligned}$ \\ 
\midrule
• & • \\ 
\bottomrule
\end{longtable} 
\end{document}

enter image description here

  • The spacing around the > symbol in the first row is quite asymmetric. Any way to improve this? – Mico Apr 2 '17 at 7:08
  • \renewcommand\arraystretch{2.5} stretched everything in the rest of table. Can I just limit its effect in a local ? And what's the difference between \hline, \cline and \toprule, \midrule, \bottomrule? – Nguyen Bao Apr 3 '17 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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