我试图让表格中的文本左对齐,并让文本按照指定的、变化的列宽自动换行。 没有:LaTex 在单词之间添加额外的空格以到达列的另一侧(右侧)。
我的两种主要方法是使用tabular
和tabularx
环境,但两种方法最终都会将单词从左到右展开,最后一行除外。
MWE 表格环境:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\section{Introduction}
test
\begin{table}[]
%\begin{tabular}{|l|l|l|l|l|l|l|}
\hspace*{-11.50em}
\begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}
\hline
Week2Lecture2 ANN: Perceptrons, MLP and Backpropagation & 1. 1.a. Neural Networks Supervised & 2. 1.b. Neural Networks UnSupervised & 9. 5.a. Notation & & & \\ \hline
W2L3 ANN: Training and building MLPs & 1. 1.a. Neural Networks Supervised & 2. 1.b. Neural Networks UnSupervised & 12. 5.d Gradient descent & 13. 5.e Training Neural Networks & 10. 5.b. Model Architecture & 11. 5.c Loss Function \\ \hline
W3L4 Reinforcement Learning & 4. 2.a. Reinforcement Learning & & & & & \\ \hline
W3L5 Genetic Algorithms & 3. 1.c Evolutionairy Computing & 5. 3.a. Evolutionairy computing & & & & \\ \hline
W4L6 Swarm Intelligence & 6. 3.b. Swarm Intelligence & & & & & \\ \hline
W4L7 CI Math and Principles Overview & 9. 5.a. Notation & 7. 3.c Bayesian Reasoning & & & & \\ \hline
W5L8 ANN: Unsupervised & 2. 1.b. Neural Networks UnSupervised & & & & & \\ \hline
W5L9 ANN: Deep/Recurrent and Gradient Descent & 12. 5.d Gradient descent & 4. 2.a. Reinforcement Learning & 11. 5.c Loss Function & & & \\ \hline
W6L10 Evolutionary Strategies, Genetic Programming & 5. 3.a. Evolutionairy computing & 3. 1.c Evolutionairy Computing & & & & \\ \hline
W6L11 Mixed CI strategies & All & & & & & \\ \hline
W6L12 (Werkcollege) Swarm routing & 6. 3.b. Swarm Intelligence & & & & & \\ \hline
\end{tabular}
\end{table}
\end{document}
产量: 如您所见,例如单元格 1,1 中的单词“Perceptrons, MLP”被拉长了。
MWE Tabularx 环境:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{caption}
\begin{document}
\section{Introduction}
test
\begin{table}[htbp]
\caption{attempt 11 fix column width}
\hspace*{-13.2em}
%\begin{tabularx}{1.7\columnwidth}{@{}|l|X@{}|}
\begin{tabularx}{1.6\columnwidth}{|p{3cm}|p{3cm}|}
\toprule
\multicolumn{1}{c}{years} & \multicolumn{1}{c}{description} \\
\midrule
\hline
1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\ \toprule %\hline %\addlinespace
\bottomrule
1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\ \hline \addlinespace
1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\ \hline \addlinespace
\bottomrule
\end{tabularx}
\label{tab:res}%
\end{table}
\end{document}
这也会传播这些词语。任何关于如何防止这些词语传播的建议,同时保持每行单词相同、列宽固定且单词分隔符相同,都将不胜感激!
答案1
正如@leandriis 所指出的,解决方案在第一个链接中进行了解释。为了完整地应用于这个问题,它变为:
\documentclass{article}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\section{Introduction}
test
\begin{table}[]
%\begin{tabular}{|l|l|l|l|l|l|l|}
\hspace*{-11.50em}
%\begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}
\begin{tabular}{| L{3cm} |L{3cm}|L{2.5cm}|L{2cm}|L{2.cm}|L{2.cm}|L{2.cm}|}
\hline
Week2Lecture2 ANN: Perceptrons, MLP and Backpropagation & 1. 1.a. Neural Networks Supervised & 2. 1.b. Neural Networks UnSupervised & 9. 5.a. Notation & & & \\ \hline
W2L3 ANN: Training and building MLPs & 1. 1.a. Neural Networks Supervised & 2. 1.b. Neural Networks UnSupervised & 12. 5.d Gradient descent & 13. 5.e Training Neural Networks & 10. 5.b. Model Architecture & 11. 5.c Loss Function \\ \hline
W3L4 Reinforcement Learning & 4. 2.a. Reinforcement Learning & & & & & \\ \hline
W3L5 Genetic Algorithms & 3. 1.c Evolutionairy Computing & 5. 3.a. Evolutionairy computing & & & & \\ \hline
W4L6 Swarm Intelligence & 6. 3.b. Swarm Intelligence & & & & & \\ \hline
W4L7 CI Math and Principles Overview & 9. 5.a. Notation & 7. 3.c Bayesian Reasoning & & & & \\ \hline
W5L8 ANN: Unsupervised & 2. 1.b. Neural Networks UnSupervised & & & & & \\ \hline
W5L9 ANN: Deep/Recurrent and Gradient Descent & 12. 5.d Gradient descent & 4. 2.a. Reinforcement Learning & 11. 5.c Loss Function & & & \\ \hline
W6L10 Evolutionary Strategies, Genetic Programming & 5. 3.a. Evolutionairy computing & 3. 1.c Evolutionairy Computing & & & & \\ \hline
W6L11 Mixed CI strategies & All & & & & & \\ \hline
W6L12 (Werkcollege) Swarm routing & 6. 3.b. Swarm Intelligence & & & & & \\ \hline
\end{tabular}
\end{table}
\end{document}
产量:
其精髓就在于此:
\begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}
必须改为:
\begin{tabular}{| L{3cm} |L{3cm}|L{2.5cm}|L{2cm}|L{2.cm}|L{2.cm}|L{2.cm}|}
同时添加所描述的新列类型和array
包。