1

I want some fractions in longtable cells. I use this code:

\documentclass{article}
\usepackage{longtable,amsmath}

\begin{document}
\begin{longtable}{|c|c|}
\hline 
\textbf{Function} & \textbf{Derivative}\\
\hline
$\dfrac{1}{x}$ & $-\dfrac{1}{x^2}$\\
\hline 
\end{longtable} 
\end{document}

Unfortunately:

  • the upper part of the numerator and
  • the lower part of the denominator

are intersected of the hline, as you can see below:

enter image description here

What can I do?

Thanks in advance!

1
  • 2
    Maybe you can make use of the cellspace package.
    – leandriis
    Sep 11, 2019 at 7:44

4 Answers 4

2

The easiest solution would be to place something invisible that is bigger, but that is hard to do in a consistent manner (\rule[-2ex]{0pt}{5.5ex} for example). The following defines a macro (\addpad) that adds 0.5ex of vertical padding on both the height and the depth of its argument (it has an optional argument with which you can change the padding amount).

I also used \extrarowheight from the array package so that the horizontal rules don't almost touch the upper case letters in normal-height cells.

The following uses grabbox so that it should work on arbitrary arguments (including verbatim material), but grabbox isn't necessary for the approach to work (see below).

\documentclass{article}
\usepackage{longtable,amsmath}
\usepackage{array}
\usepackage{grabbox}
\setlength\extrarowheight{1pt}

\makeatletter
\newsavebox\addpad@Box
\newcommand\addpad[1][.5ex]
  {%
    \leavevmode
    \@grabbox{}\addpad@Box{}\hbox{}
      {%
        \vrule
          height \dimexpr\ht\addpad@Box+#1
          depth 0pt
          width 0pt
        \unhcopy\addpad@Box
        \vrule
          height 0pt
          depth \dimexpr\dp\addpad@Box+#1
          width 0pt
      }%
  }
\makeatother

\begin{document}
\begin{longtable}{|c|c|}
\hline 
\textbf{Function} & \textbf{Derivative}\\
\hline
  \addpad{$\dfrac{1}{x}$} & $-\dfrac{1}{x^2}$\\
\hline 
\end{longtable} 
\end{document}

enter image description here

Version without grabbox

\documentclass{article}
\usepackage{longtable,amsmath}
\usepackage{array}
\setlength\extrarowheight{1pt}

\newsavebox\addpadBox
\newcommand\addpad[2][.5ex]
  {%
    \sbox\addpadBox{#2}%
    \ht\addpadBox\dimexpr\ht\addpadBox+#1
    \dp\addpadBox\dimexpr\dp\addpadBox+#1
    \usebox\addpadBox
  }

\begin{document}
\begin{longtable}{|c|c|}
\hline 
\textbf{Function} & \textbf{Derivative}\\
\hline
  \addpad{$\dfrac{1}{x}$} & $-\dfrac{1}{x^2}$\\
\hline 
\end{longtable} 
\end{document}
2
  • thanks a lot!!! Problem solved! Sep 11, 2019 at 8:18
  • @KώσταςΚούδας, other answers solves your problem too ;-)
    – Zarko
    Sep 11, 2019 at 8:52
2

You have for that the cellspace package, which defines minimal vertical spacings at the top and bottom of cells in columns with specifier prefixed with e the letter S (or C if you load siunitx. It also works in the amsmath matrix environments with the option [math].

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

\begin{document}

\begin{longtable}{|Sc|Sc|}
\hline
\textbf{Function} & \textbf{Derivative}\\
\hline
$\dfrac{1}{x}$ & $-\dfrac{1}{x^2}$\\
\hline
\end{longtable}

\end{document} 

enter image description here

1

One more (simple) solution with use of the makecell package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{makecell, longtable}
\setcellgapes{5pt}

\begin{document}
{ \makegapedcells
\begin{longtable}{|c|c|}
\hline
\textbf{Function} & \textbf{Derivative}\\
\hline
$\dfrac{1}{x}$ & $-\dfrac{1}{x^2}$\\
\hline
\end{longtable}
}
\end{document}

enter image description here

1

Another possibilty can be using \arraystretch{...}, where all the cells have the same vertical lenght.

enter image description here

\documentclass{article}
\usepackage{longtable,amsmath}

\begin{document}
\renewcommand\arraystretch{3}
\begin{longtable}{|c|c|}
\hline 
\textbf{Function} & \textbf{Derivative}\\
\hline

$\dfrac{1}{x}$ & $-\dfrac{1}{x^2}$\\
\hline 
\end{longtable}
\end{document}

You must log in to answer this question.

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