longtable 中用底線換行

longtable 中用底線換行

我有以下程式碼:

\begin{landscape}

\setlength\tabcolsep{3pt}
\begin{longtable}{m{0.5cm}|m{3cm}|m{3cm}|m{3cm}|m{9cm}}
\hline
\textbf{C1}&\textbf{Col2}&\textbf{Col3}&\textbf{Col4}&\textbf{Col5}\\
\hline
\endfirsthead
\endfoot
1&Self-report&-&D\_alcohol\_consumption&\\\hline
2&Self-report&-&Trastorno del pensamiento controlado&\\\hline
3&Self-report&-&D-alcohol-consumption&\\\hline
\end{longtable} 

\end{landscape}

它產生這個表

在此輸入影像描述

我需要在“Col4”列中放置很長的變數名稱,其中許多變數名稱都有下劃線,例如第一行。但它不適合單元格並延伸到“Col5”。我期望的是像第二行一樣自動換行。

我認為這與下劃線有關,因為在第三行中我用連字號替換了它們,這正是我所期望的。但我需要使用底線。

有什麼建議保留下劃線嗎?

答案1

基本上,也許並不奇怪,到目前為止,沒有人告訴 LaTeX 如何連字符“單字”

D\_alcohol\_consumption

幸運的是,有一種簡單的方法可以告訴 LaTeX 它可以將子字串consumption視為單字:只需替換D\_alcohol\_consumption

D\_alcohol\_\hspace{0pt}consumption

另一個註解:預設情況下,LaTeX 不會對包含一個或多個-(連字符)實例的單字使用連字符。如果你想讓LaTeX對consumption複合連字符中的單字進行連字符D-alcohol-consumption,你需要做的就是將其重寫為

D-alcohol-\hspace{0pt}consumption

最後的評論:鑑於某些列的狹窄,我會為這些列使用不規則的右側佈局,而不是完全對齊(這是預設),同時仍允許在需要時使用連字符。這可以透過在列說明符前添加>{\RaggedRight};來自動實現。請參閱下面的程式碼以了解如何實現這個想法。 (如果您確實想抑制連字符,只需替換>{\RaggedRight}>{\raggedright\arraybackslash}.


在此輸入影像描述

\documentclass{article}
\usepackage{array}
\usepackage{ragged2e} % for '\RaggedRight' macro
\begin{document}

\begin{tabular}{@{} >{\RaggedRight}p{3.1cm} @{}}
\hline
D\_alcohol\_\hspace{0pt}consumption \\ \hline
D-alcohol-consumption \\               \hline
D-alcohol-\hspace{0pt}consumption \\   \hline
\end{tabular}

\end{document}

答案2

我透過使用連字符包發現了

\usepackage{hyphenat}
\begin{landscape}

\setlength\tabcolsep{3pt}
\begin{longtable}{m{0.5cm}|m{3cm}|m{3cm}|m{3cm}|m{9cm}}
\hline
\textbf{C1}&\textbf{Col2}&\textbf{Col3}&\textbf{Col4}&\textbf{Col5}\\
\hline
\endfirsthead
\endfoot
1&Self-report&-&D\_alcohol\_consumption&\\\hline
2&Self-report&-&Trastorno del pensamiento controlado&\\\hline
3&Self-report&-&D-alcohol-consumption&\\\hline
\end{longtable}

\end{landscape}

在此輸入影像描述

相關內容