设置多列表格的宽度以适应页面宽度

设置多列表格的宽度以适应页面宽度

我有一个多列表格,但表格不适合页面宽度,因此无法获得此设置

在此处输入图片描述

当我运行乳胶时,也使用\begin{adjustbox}{width=\textwidth},我得到了这个结果

在此处输入图片描述

\documentclass[12pt]{article}
    \usepackage{tabularx}
    \usepackage{graphicx}
    \usepackage{adjustbox}


\begin{document}

\begin{table}[ht]
\centering
\begin{adjustbox}{width=\textwidth}
\begin{tabular}{|l|l|l|l|l|} \hline
    \textbf{Course Title} 
    & \textbf{Instructor}   
    & \textbf{Credit} 
    & \textbf{Teaching hours} 
    & \textbf{Pass grade} \\ \hline
    Programming Methodologies & 
    Prof. Alan Turing & 
    9 & 
    72 & 
    28/30 \\ \hline
    \multicolumn{5}{|l|}{\textbf{Course~Content} } \\ \hline
    \multicolumn{5}{|l|}{\begin{tabular}[c]{@{}l@{}}Oject-oriented techniques for software construction: design principles and language features in order to achieve extendibility, reusability and compatibility. Classes, Encapsulation and Information Hiding. UML (Unified Modeling Language): the class diagram for analysis, design and documentation.~Inheritance. Object composition and delegation. Abstract classes. Classes as Types. Polymorphism by subtyping and Genericity.\\Object-oriented Programming in Java. Using JUnit for automated tests. Design principles and clean code. Design Patterns.\end{tabular}} \\ \hline
\end{tabular}
\end{adjustbox}
\end{table}


\end{document}

答案1

您的表格太宽,您必须使用 p 列引入换行符。最好减小整个表格的字体大小,避免这样做,\adjustbox因为它会使字体变形。参见示例 2。

更好的解决方案是使用表格并结合普通 LR 柱和表格C专栏。表格能够正确计算列空间,正如您将从示例 1 中看到的那样。

当然,如果您有多个类似的表格,最好使用固定宽度的列,以便所有表格看起来都类似。

示例 1 -表格

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tabulary, ragged2e}
\setlength{\extrarowheight}{2pt}

\begin{document}
\begin{table}[!ht]
\centering
\footnotesize
\begin{tabulary}{\linewidth}{|*{2}{l|}c|*{2}{C|}} \hline
    \textbf{Course Title} 
    & \textbf{Instructor}   
    & \textbf{Credit} 
    & \textbf{Teaching hours} 
    & \textbf{Pass grade} \\ \hline
    Programming Methodologies & 
    Prof. Alan Turing & 
    9 & 
    72 & 
    28/30 \\ \hline
    \multicolumn{5}{|l|}{\textbf{Course~Content} } \\ \hline
    \multicolumn{5}{|>{\RaggedRight\arraybackslash}p{\dimexpr \linewidth - 2\tabcolsep - 2\arrayrulewidth}|}{\emph{Object-oriented techniques for software construction}: design principles and language features in order to achieve extendibility, reusability and compatibility. Classes, Encapsulation and Information Hiding. \newline
        \emph{UML (Unified Modeling Language)}: the class diagram for analysis, design and documentation.~Inheritance. Object composition and delegation. Abstract classes. Classes as Types. Polymorphism by subtyping and Genericity.\newline
        \emph{Object-oriented Programming in Java}: Using JUnit for automated tests. Design principles and clean code. Design Patterns.} \\ \hline
\end{tabulary}
\end{table}
\end{document}

示例 2

在此处输入图片描述

\documentclass[12pt]{article}
    \usepackage{tabularx}
    \usepackage{graphicx}
    \usepackage{adjustbox}


\begin{document}

\begin{table}[ht]
\centering
\footnotesize
\begin{tabular}{|p{3cm}|l|l|p{1.5cm}|l|} \hline
    \textbf{Course Title} 
    & \textbf{Instructor}   
    & \textbf{Credit} 
    & \textbf{Teaching hours} 
    & \textbf{Pass grade} \\ \hline
    Programming Methodologies & 
    Prof. Alan Turing & 
    9 & 
    72 & 
    28/30 \\ \hline
    \multicolumn{5}{|l|}{\textbf{Course~Content} } \\ \hline
    \multicolumn{5}{|p{\dimexpr \linewidth - 2\tabcolsep - 2\arrayrulewidth}|}{Object-oriented techniques for software construction: design principles and language features in order to achieve extendibility, reusability and compatibility. Classes, Encapsulation and Information Hiding. UML (Unified Modeling Language): the class diagram for analysis, design and documentation.~Inheritance. Object composition and delegation. Abstract classes. Classes as Types. Polymorphism by subtyping and Genericity.\\Object-oriented Programming in Java. Using JUnit for automated tests. Design principles and clean code. Design Patterns.\end{tabular}} \\ \hline
\end{tabular}
\end{table}
\end{document}

答案2

tabularx和 的解决方案makecell

\documentclass[12pt]{article}
\usepackage{tabularx, makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.2pt}

\begin{document}

\begin{table}[ht]
\centering
\setlength{\extrarowheight}{2pt}
\begin{tabularx}{\linewidth}{|>{\hsize=0.9\hsize}X|>{\hsize=1.1\hsize}X|l|l|l|} \hline
    \textbf{Course Title}
    & \textbf{Instructor}
    & \textbf{Credit}
    & \thead[l]{Teaching\\ hours}
    & \thead[l]{Pass grade} \\ \hline
    \makecell[l]{Programming\\ Methodologies} &
    Prof. Alan Turing &
    9 &
    72 &
    28/30 \\ \hline
    \multicolumn{5}{|l|}{\textbf{Course~Content}} \\ \hline
 \multicolumn{5}{|p{\dimexpr\textwidth-2\tabcolsep}|}{Object-oriented techniques for software construction: design principles and language features in order to achieve extendibility, reusability and compatibility. Classes, Encapsulation and Information Hiding. UML (Unified Modeling Language): the class diagram for analysis, design and documentation.~Inheritance. Object composition and delegation. Abstract classes. Classes as Types. Polymorphism by subtyping and Genericity.\par Object-oriented Programming in Java. Using JUnit for automated tests. Design principles and clean code. Design Patterns. } \\ \hline
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

相关内容