表格中的文本左对齐

表格中的文本左对齐

我对 LaTeX 还很陌生,抱歉。

我使用从不同的 tex.stackexchange 问题中拼凑起来的代码创建了一个表格。这是我的表格:

\documentclass{article}

% packages needed
\usepackage{graphicx}
\usepackage{amsmath}
%\usepackage{cite}
\usepackage{siunitx}
\usepackage[hidelinks]{hyperref}
\usepackage{enumerate}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage[round]{natbib}
\usepackage[
singlelinecheck=false % <-- important
]{caption}
\usepackage{enumitem}

\newlist{tabitem}{itemize}{1}
\setlist[tabitem]{wide=0pt, nosep, leftmargin= * ,label=\textbullet,after=\vspace{-\baselineskip},before=\vspace{-0.6\baselineskip}}

\usepackage[nottoc]{tocbibind}

\begin{document}


\setlength{\arrayrulewidth}{0.7mm}
\setlength{\tabcolsep}{20pt}
\renewcommand{\arraystretch}{2}
{
\begin{table}[h]
\resizebox{\textwidth}{!}{%
\begin{tabular}{ |p{1.5cm}|p{3cm}|p{1.5cm}|p{3cm}|p{4cm}|p{3cm}|  }
\hline
Variable& Description &Units &Parameters/References & Notes & Programs Used \\
\hline
Topographic position index (TPI)
& 

Comparison of elevation in a cell compared to mean elevation of a specified neighborhood around that cell

& 

unitless
&

\begin{tabitem}
  \item Method = \citep{Weiss2001}
  \item Radius = 10, 25, 50, 100, 150, 300, 500, 1000, 2000
\end{tabitem}
& 

\begin{tabitem}
  \item different resolutions used due to computational rigor
  \item 5 m resolution (TPI10 to TPI300)
  \item 15 m resolution (TPI500)
  \item 30 m resolution (TPI100 to TPI2000)
\end{tabitem}
& 

\begin{tabitem}
  \item ArcGIS - Geomorphometry and Gradient Metrics Toolbox \citep{Evans2014}
\end{tabitem}\\
\hline

Slope
& 

Proxies for water flow, snow movements, erosion, and solar radiation \cite{Leempoel2015}

& 

degrees
&

\begin{tabitem}
  \item Method = \citep{Horn1981}
  \item \cite{Lecours2017}
\end{tabitem}
& 

\begin{tabitem}
  \item Better metric in complex terrain than \citep{Zevenbergen1987} \cite{Lecours2017}
\end{tabitem}
& 

\begin{tabitem}
  \item ArcGIS - TASSE Toolbox \citep{Lecours2017}
\end{tabitem}\\
\cline{1-1}
\cline{3-6}

Aspect
& 


& 

radians
&

\begin{tabitem}
  \item Method = \cite{Horn1981}
  \item \cite{Lecours2017}
\end{tabitem}
& 

\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
& 

\begin{tabitem}
  \item ArcGIS - TASSE Toolbox \citep{Lecours2017}
\end{tabitem}\\
\hline

Topographic wetness index (TWI)
& 

Quantifies topographic control on hydrological processes

& 

radians
&

\begin{tabitem}
  \item Method = \citep{Beven1979}
\end{tabitem}
& 

\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
& 

\begin{tabitem}
  \item QGIS - SAGA Tools
\end{tabitem}\\
\hline

Solar radiation
& 

\begin{tabitem}
    \item Poor durability
    \item Poor gripping strength for soft materials
\end{tabitem}
& 

radians
&

\begin{tabitem}
  \item Method = \citep{Beven1979}
\end{tabitem}
& 

\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
& 

\begin{tabitem}
  \item QGIS - SAGA Tools
\end{tabitem}

\tabularnewline
\hline
\end{tabular}}
\caption{ test}
\label{table:test}
\end{table}
}

\end{document}

我想将单元格内容左对齐,但这样做时出现错误。我意识到左对齐将由以下行控制:

\begin{tabular}{ |p{1.5cm}|p{3cm}|p{1.5cm}|p{3cm}|p{4cm}|p{3cm}| }

我的理解是,此行中的 p 使水平对齐完整(LaTeX 表格功能

我尝试将此行更改为:

\begin{tabular}{ | l | l | l | l | l | l | }

但随后我收到错误。我认为这可能是由于以下行造成的:

\resizebox{\textwidth}{!}{%然后结束于\end{tabular}}。这是我在 tex.stackexchange 上找到的一行,用于调整表格的尺寸以完美适合您的文档。

任何建议都将非常感谢!

答案1

一种可能的方法是:

在此处输入图片描述

(红线表示文本边框,在实际文档中删除包,showframe如 MWE 中的评论所述)

在 MWE (最小工作示例) 中与您的 MWE 相比,进行了以下更改:

  • 已删除\resizebox,因为有了它您将无法控制表格字体大小。
  • 对于表格环境,使用的tabularx宽度等于\textwidth
  • 定义新的列类型,使单元格的内容左对齐。

编辑

  • 命令\citep未在包中定义cite,因此我将其替换为命令cite。 您可以将\cite包替换为其他包,例如natbib,定义包\citep
  • 现在我添加了两个附加包:makecell,通过该宏makegapedcells可以在单元格中添加更多垂直空间,以及multirow,这使得可以在更正的 MWE 中创建多行单元格。
  • 还添加了siunitx用于编写单元的包(例如5 m,参见 MWE)
  • 字体大小从footnotesize增加到\small
  • 不清楚,中的数字是什么意思Radius = 10, 25, 50, 100, 150, 300, 500, 1000, 2000。半径不是无单位的......
  • 无关:
    • 表格标题通常位于表格上方
    • MWE 中的注释旨在描述代码和注释,描述这个特定的 MWE 的具体内容以及当您将其合并到实际文档中时应该做什么。
\documentclass{article}
\usepackage[margin=25mm]{geometry} % set suitable page layour
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

% packages needed in this MWE
\usepackage{cite}
\usepackage{siunitx}
% \usepackage{enumerate} % should not be loaded when you use enumitem 

\usepackage[skip=0.33\baselineskip, % new
            font=footnotesize,      % new
            labelfont=bf,           % new
            singlelinecheck=false]{caption}
\usepackage{enumitem}
\newlist{tabitem}{itemize}{1}
\setlist[tabitem]{nosep, 
                leftmargin=*, 
                label=\textbullet,
                after=\vspace{-\baselineskip},
                before=\vspace{-0.6\baselineskip}}
\usepackage{ragged2e}
\usepackage{makecell, multirow,     % new
            tabularx}
\newcolumntype{L}[1]{>{\hsize=#1\hsize\linewidth=\hsize% for left aligning of cells contents
                       \RaggedRight}X} 
\usepackage{siunitx}    % new, for writing units
\usepackage[hidelinks]{hyperref}


\begin{document}
    \begin{table}[h]
    \setlength\tabcolsep{4pt}
    \footnotesize
\begin{tabularx}{\linewidth}{|L{0.7}|L{1.3}|l|L{1}|L{1.2}|L{0.8}|  }
    \hline
Variable& Description &Units &Parameters/\newline References & Notes & Programs Used \\
    \hline
Topographic position index (TPI)
&
Comparison of elevation in a cell compared to mean elevation of a specified neighborhood around that cell
&
unitless
&
\begin{tabitem}
  \item Method = \cite{Weiss2001}
  \item Radius = 10, 25, 50, 100, 150, 300, 500, 1000, 2000
\end{tabitem}
&
\begin{tabitem}
  \item different resolutions used due to computational rigor
  \item \SI{5}{m} resolution (TPI10 to TPI300)
  \item \SI{15}{m} resolution (TPI500)
  \item \SI{30}{m} resolution (TPI100 to TPI2000)
\end{tabitem}
&
\begin{tabitem}
  \item ArcGIS - Geomorphometry and Gradient Metrics Toolbox \cite{Evans2014}
\end{tabitem}\\
    \hline
Slope
&   \multirow[t]{7}{=}{   % defined in `multirow` package
Proxies for water flow, snow movements, erosion, and solar radiation \cite{Leempoel2015}}
&
degrees
&
\begin{tabitem}
  \item Method = \cite{Horn1981}
  \item \cite{Lecours2017}
\end{tabitem}
&
\begin{tabitem}
  \item Better metric in complex terrain than \cite{Zevenbergen1987} \cite{Lecours2017}
\end{tabitem}
&
\begin{tabitem}
  \item ArcGIS - TASSE Toolbox \cite{Lecours2017}
\end{tabitem}\\
    \cline{1-1}
    \cline{3-6}
Aspect
&
&
radians
&
\begin{tabitem}
  \item Method = \cite{Horn1981}
  \item \cite{Lecours2017}
\end{tabitem}
&
\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
&
\begin{tabitem}
  \item ArcGIS - TASSE Toolbox \cite{Lecours2017}
\end{tabitem}\\
    \hline
Topographic wetness index (TWI)
&
Quantifies topographic control on hydrological processes
&
radians
&
\begin{tabitem}
  \item Method = \cite{Beven1979}
\end{tabitem}
&
\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
&
\begin{tabitem}
  \item QGIS - SAGA Tools
\end{tabitem}\\
    \hline
Solar radiation
&
\begin{tabitem}
    \item Poor durability
    \item Poor gripping strength for soft materials
\end{tabitem}
&
radians
&
\begin{tabitem}
  \item Method = \cite{Beven1979}
\end{tabitem}
&
\begin{tabitem}
  \item Northernness (cosine aspect)
  \item Easternness (sine of aspect)
\end{tabitem}
&
\begin{tabitem}
  \item QGIS - SAGA Tools
\end{tabitem}\\
    \hline
\end{tabularx}
\caption{Test}
\label{table:test}
    \end{table}
\end{document}

相关内容