如何在其中一个单元格中添加左侧额外填充

如何在其中一个单元格中添加左侧额外填充

我想在几个单元格中添加一些额外的空间(而不是整个列),但我不知道该怎么做。我在 SE 中阅读了许多答案,但我找到的答案都涉及如何在打开环境中定义自定义空间并使用@{}

我需要的是特定单元格左侧的额外空间,而不是全部。

我认为我可以添加一个额外的列,并在需要额外填充时将其留空。我使用 \multicolumn 合并不需要填充的列,并在相反情况下跳过一列。

在 MWE 中,第一个表格没有空格。第二个表格有空格,正如我想要的那样,但是第三个表格显示,如果合并单元格的内容很长,表格就会溢出。

还有其他方法吗?

例子:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[usernames,dvipsnames,svgnames,table]{xcolor}

\setlength{\arrayrulewidth}{0.5pt}
\setlength{\tabcolsep}{6pt}
\renewcommand{\arraystretch}{1.3}

\newcommand{\thickrule}{\noalign{\hrule height 1.5pt}}
\newcommand{\thinrule}{\noalign{\hrule height 0.5pt}}
\newcommand{\maintask}[3]{\rowcolor{lgray} #1 & #2 & #3}
\newcommand{\subtask}[3]{ #1 & #2 & #3}
\newcommand{\maintaska}[3]{\rowcolor{lgray} #1 & #2 & \multicolumn{2}{l}{#3}}
\newcommand{\subtaska}[3]{ #1 & #2 & & #3}


\definecolor{lgray}{gray}{0.90}

\begin{document}

Table 1

\begin{table}[h!]
\centering
\begin{tabular}{ c c p{0.7\textwidth} }
\thickrule
\rule{0pt}{18pt}
\textbf{Start} & \textbf{End} & \multicolumn{1}{c}{\textbf{Tasks}} \\[6pt]
\thickrule
\maintask{03/2017}{06/2019}{Main task 1} \\ \thinrule
\maintask{02/2017}{12/2018}{Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 2.1} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 2.2} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 2.3} \\ \thinrule
\maintask{03/2017}{06/2019}{Main task 3} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 3.1} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 3.2} \\ \thinrule
\maintask{03/2017}{06/2019}{Main task 4} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 4.1} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 4.2} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 4.3} \\ \thinrule
\maintask{06/2018}{06/2019}{Main task 5} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 5.1} \\ \thinrule
\subtask{02/2017}{06/2018}{Subtask 5.2} \\ \thinrule
\maintask{03/2017}{06/2019}{Main task 6} \\ \thickrule
\end{tabular}
\caption{Plan}
\label{tab:phdplan}
\end{table}

Table 2

\begin{table}[h!]
\centering
\begin{tabular}{ c c p{0.63cm} p{0.6\textwidth} }
\thickrule
\rule{0pt}{18pt}
\textbf{Start} & \textbf{End} & \multicolumn{2}{c}{\textbf{Tasks}} \\[6pt]
\thickrule
\maintaska{03/2017}{06/2019}{Main task 1} \\ \thinrule
\maintaska{02/2017}{12/2018}{Main task 2.} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.2} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.3} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 3} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 3.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 3.2} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 4} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.2} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.3} \\ \thinrule
\maintaska{06/2018}{06/2019}{Main task 5} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 5.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 5.2} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 6} \\ \thickrule
\end{tabular}
\caption{Plan}
\label{tab:phdplan}
\end{table}

\begin{table}[h!]
\centering
\begin{tabular}{ c c p{0.63cm} p{0.6\textwidth} }
\thickrule
\rule{0pt}{18pt}
\textbf{Start} & \textbf{End} & \multicolumn{2}{c}{\textbf{Tasks}} \\[6pt]
\thickrule
\maintaska{03/2017}{06/2019}{Main task 1} \\ \thinrule
\maintaska{02/2017}{12/2018}{Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2 Main task 2} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1 subtask 2.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.2} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 2.3} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 3} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 3.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 3.2} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 4} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.2} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 4.3} \\ \thinrule
\maintaska{06/2018}{06/2019}{Main task 5} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 5.1} \\ \thinrule
\subtaska{02/2017}{06/2018}{subtask 5.2} \\ \thinrule
\maintaska{03/2017}{06/2019}{Main task 6} \\ \thickrule
\end{tabular}
\caption{Plan}
\label{tab:phdplan}
\end{table}

\end{document}

答案1

您需要将的定义更改\maintaska为:

\newcommand{\maintaska}[3]{\rowcolor{lgray} #1 & #2 & \multicolumn{2}
     {p{\dimexpr0.6cm+2\tabcolsep+0.6\linewidth\relax}}{#3}}  % <--- changed is column type

在此处输入图片描述

相关内容