3

This is my table:

enter image description here

And i want this table has format like this (the double hline after title should be there i forgot to add and there is no vertical line also):

enter image description here

Btw, i'm using repeated table head and trying to use p{x cm} but error message appears. It said Package array error bla bla bla. Please help me. Thanks!

MWE:

\documentclass[a4paper,12pt,oneside,openany]{book}
\usepackage[a4paper, inner=4cm, outer=3cm, top=4cm, bottom=3cm]{geometry}
\usepackage{setspace,amsmath,times,array,caption,lipsum,longtable}
\hyphenpenalty=10000
\sloppy
\usepackage[utf8]{inputenc}
\usepackage[column=O]{cellspace}
\linespread{1.5}
\begin{document}
        \begin{center}
        \renewcommand{\arraystretch}{1.5}
        \begin{longtable}{cl}
            \caption{ODE Examples}\\
            \hline
            \multicolumn{1}{c}{\centering \textbf{ODE}}
            & \multicolumn{1}{c}{\centering \textbf{Classification}}\\
            \hline \hline
            \endfirsthead
            \hline
            \multicolumn{1}{c}{\centering \textbf{ODE}}
            & \multicolumn{1}{c}{\centering \textbf{Classification}}\\
            \hline \hline
            \endhead 
            $\dfrac{\mathrm{d} y}{\mathrm{d} x} = y+x$ & First Order ODE \\
            \hline
            $\left(\dfrac{\mathrm{d}^2 y}{\mathrm{d} x^2}\right)^2 + 2\dfrac{\mathrm{d} y}{\mathrm{d} x} -3y = 0$ & Second order ODE \\
            \hline
        \end{longtable}
    \end{center}
\lipsum
\end{document}
4
  • something like the answer below -- 0.5\textwidth is added in the definition of the table to spread each of the two columns to 50 percent of the textwidth -- makecell package and cellgape is used to give a gap between the row line and the maths content
    – js bibra
    Feb 1, 2021 at 5:49
  • Remove the center environment around the longtable.
    – leandriis
    Feb 1, 2021 at 6:02
  • @user516076 -- centering of the first column added also -- forgot to do that
    – js bibra
    Feb 1, 2021 at 6:13
  • You already load the cellspace package. Why don't you use this package to adjust the height of your table rows?
    – leandriis
    Feb 1, 2021 at 6:29

2 Answers 2

3

enter image description here

\documentclass[a4paper,12pt,oneside,openany]{book}
\usepackage[a4paper, inner=4cm, outer=3cm, top=4cm, bottom=3cm]{geometry}
\usepackage{setspace,amsmath,times,array,caption,lipsum,longtable}
\hyphenpenalty=10000
\sloppy
\usepackage[utf8]{inputenc}
\usepackage{makecell}

\begin{document}
{ \setcellgapes{4pt}\makegapedcells%
        \begin{longtable}{p{0.5\textwidth}|p{0.5\textwidth}}
            \caption{ODE Examples}\\
            \hline
            \multicolumn{1}{c|}{\textbf{ODE}}
            & \multicolumn{1}{|c}{\textbf{Classification}}\\
            \hline \hline
            \endfirsthead
            \hline
            \multicolumn{1}{c}{\textbf{ODE}}
            & \multicolumn{1}{c}{\textbf{Classification}}\\
            \hline \hline
            \endhead 
            $\dfrac{\mathrm{d} y}{\mathrm{d} x} = y+x$ & First Order ODE \\
            \hline
            $\left(\dfrac{\mathrm{d}^2 y}{\mathrm{d} x^2}\right)^2 + 2\dfrac{\mathrm{d} y}{\mathrm{d} x} -3y = 0$ & Second order ODE \\
            \hline
        \end{longtable}
}
\lipsum
\end{document}

edit -- for the centering of the first column contents

added -- \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

enter image description here

\documentclass[a4paper,12pt,oneside,openany]{book}
\usepackage[a4paper, inner=4cm, outer=3cm, top=4cm, bottom=3cm]{geometry}
\usepackage{setspace,amsmath,times,array,caption,lipsum,longtable}
\hyphenpenalty=10000
\sloppy
\usepackage[utf8]{inputenc}
\usepackage{makecell}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
    { \setcellgapes{4pt}\makegapedcells%
        \begin{longtable}{P{0.5\textwidth}|p{0.5\textwidth}}
            \caption{ODE Examples}\\
            \hline
            \multicolumn{1}{c|}{\textbf{ODE}}
            & \multicolumn{1}{|c}{\textbf{Classification}}\\
            \hline \hline
            \endfirsthead
            \hline
            \multicolumn{1}{c}{\textbf{ODE}}
            & \multicolumn{1}{c}{\textbf{Classification}}\\
            \hline \hline
            \endhead 
            $\dfrac{\mathrm{d} y}{\mathrm{d} x} = y+x$ & First Order ODE \\
            \hline
            $\left(\dfrac{\mathrm{d}^2 y}{\mathrm{d} x^2}\right)^2 + 2\dfrac{\mathrm{d} y}{\mathrm{d} x} -3y = 0$ & Second order ODE \\
            \hline
        \end{longtable}
    }
    \lipsum
\end{document}
1
  • Your table will be wider than the textwidth since you did not take into account the value of tabcolsep that is added to either side of the table nor the width of the vertical line. You can also see this in the screenshot you added. Notice how the horizontal lines are wider than the text block below the table.
    – leandriis
    Feb 1, 2021 at 6:26
3

You can reduce the height of the header by adding negative vertical space. I added new column types for better control of the width of the columns, if needed.

The rules in the booktabs package also vertically expand cells for a cleaner appearance.

xy

% !TeX TS-program = pdflatex

\documentclass[a4paper,12pt,oneside,openany]{book}
\usepackage[a4paper, inner=4cm, outer=3cm, top=4cm, bottom=3cm]{geometry}
\usepackage{setspace,amsmath,times,array,caption,lipsum,longtable}
\hyphenpenalty=10000
\sloppy
\usepackage[utf8]{inputenc}
\usepackage[column=O]{cellspace}

\usepackage{booktabs}

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}m{#1\textwidth}}
\newcolumntype{Y}[1]{>{\centering\arraybackslash}m{#1\textwidth}}

\linespread{1.5}
\begin{document}    

%\begin{center}
        \renewcommand{\arraystretch}{1.5}
    \begin{longtable}{Y{0.5}P{0.25}}% <<< set the columns widths
        \caption{ODE Examples}\\
        \toprule \\ [-45pt]
            \textbf{ODE}   & \multicolumn{1}{c}{\bfseries Classification}\\ [-5pt]
        \midrule\midrule 
        \endfirsthead
        \toprule \\ [-45pt]
            \textbf{ODE}   & \multicolumn{1}{c}{\bfseries Classification}\\ [-5pt]
        \midrule\midrule 
        \endhead        
        $\dfrac{\mathrm{d} y}{\mathrm{d} x} = y+x$ &  {First Order ODE}\\
        \midrule
        $\left(\dfrac{\mathrm{d}^2 y}{\mathrm{d} x^2}\right)^2 + 2\dfrac{\mathrm{d} y}{\mathrm{d} x} -3y = 0$ & {Second order ODE} \\
        \bottomrule
    \end{longtable}
%\end{center}
\lipsum
\end{document}

You must log in to answer this question.

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