降低表头高度并向左右边距拉伸

降低表头高度并向左右边距拉伸

这是我的桌子:

在此处输入图片描述

我希望这个表格具有如下格式(hline标题后应该有双引号,我忘了添加,而且也没有垂直线):

在此处输入图片描述

顺便说一句,我正在使用重复的表头并尝试使用p{x cm},但出现错误消息。它说Package array error bla bla bla。请帮助我。谢谢!

梅威瑟:

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

答案1

在此处输入图片描述

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

编辑——使第一列内容居中

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

在此处输入图片描述

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

答案2

您可以通过添加负垂直空间来减少标题的高度。如果需要,我添加了新的列类型,以便更好地控制列的宽度。

包中的规则booktabs还垂直扩展单元格以获得更整洁的外观。

坐标

% !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}

相关内容