使用 p{} 规范在表格内换行

使用 p{} 规范在表格内换行

我正在尝试使用 p 规范来让表格中的文本换行。我查看了示例和其他问题,但无法让它工作。我用 latex 表格生成器生成了我的表格,并且使用了一些颜色,所以表格代码相当混乱。我尝试将 p 代码放入

p{40mm},

在表格中。更具体地说,为列指定代码颜色,如下所示,

\begin{table}[H]
\begin{tabular}{
>{\columncolor[HTML]{8E7CC3}} p{40mm} l 
>{\columncolor[HTML]{D9D2E9}} p{40mm} l 
>{\columncolor[HTML]{FFF2CC}} p{40mm} l 
>{\columncolor[HTML]{EA9999}} p{40mm} l}
\cline{1-3}
... more code,

但这只会使文本在其中一行换行。我在哪里设置列宽?

我在下面提供了一个工作示例(没有尝试文本换行)。

\documentclass[12pt,a4paper,swedish]{report}

    
\usepackage[table]{xcolor} % For tables (color)
\usepackage{moreverb}                               % List settings
\usepackage{textcomp}                               % Fonts, symbols etc.


\usepackage[T1]{fontenc}                            % Output settings
\usepackage[utf8]{inputenc}                         % Input settings
\usepackage{amsmath}                                % Mathematical expressions (American mathematical society)
\usepackage{amssymb}                                % Mathematical symbols (American mathematical society)
\usepackage{graphicx}                               % Figures

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables


\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=3cm]{geometry}         % Page margin lengths           

\usepackage{float}                                  % Enables object position enforcement using [H]

\usepackage{siunitx} % For units
\usepackage{gensymb} % For certain symbols such as \degree


\usepackage{parskip}                                % Enables vertical spaces correctly 



\begin{document}


\begin{table}[H]
\begin{tabular}{
>{\columncolor[HTML]{8E7CC3}}l 
>{\columncolor[HTML]{D9D2E9}}l 
>{\columncolor[HTML]{FFF2CC}}l 
>{\columncolor[HTML]{EA9999}}l}
\cline{1-3}
\multicolumn{1}{|l|}{\cellcolor[HTML]{8E7CC3}\textbf{Krav}}         & \multicolumn{1}{l|}{\cellcolor[HTML]{D9D2E9}\textbf{Mått}}                      & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}\textbf{Beskrivning}}                                                                             & \textbf{Kravtyp}             \\ \cline{1-3}
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}First thing} & $50-54\degree C$                                                           & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this} & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Second thing}                & $x mAh (5V)$                                                                      & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                                  & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Third thing}              & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                 & Krav                         \\
\cellcolor[HTML]{8E7CC3}Fourth thing                               & \cellcolor[HTML]{D9D2E9}{\color[HTML]{000000} \textgreater{}$100\degree C$} & \cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this                             & \cellcolor[HTML]{EA9999}Krav \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Fifth thing}              & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                           & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Sixth thing}             & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                              & Önskemål                     \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Seventh thing}     & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}               & Önskemål                     \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Eighth thing}  & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                               & Önskemål                    
\end{tabular}
\end{table}


\end{document}

我正在背面书写。

答案1

siunitx我意识到我在评论中所说的内容,并且使用's正确排版了单位\qty

\documentclass[12pt,a4paper,swedish]{report}

    
\usepackage[table]{xcolor} % For tables (color)
\usepackage{moreverb}                               % List settings
\usepackage{textcomp}                               % Fonts, symbols etc.


\usepackage[T1]{fontenc}                            % Output settings
\usepackage[utf8]{inputenc}                         % Input settings
\usepackage{amsmath}                                % Mathematical expressions (American mathematical society)
\usepackage{amssymb}                                % Mathematical symbols (American mathematical society)
\usepackage{graphicx}                               % Figures

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables


\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=3cm]{geometry}         % Page margin lengths           

\usepackage{float}                                  % Enables object position enforcement using [H]

\usepackage{siunitx} % For units
\usepackage{gensymb} % For certain symbols such as \degree


\usepackage{parskip}                                % Enables vertical spaces correctly 


\begin{document}


\begin{table}[H]
\begin{tabular}{
|>{\columncolor[HTML]{8E7CC3}}p{3cm}
|>{\columncolor[HTML]{D9D2E9}}p{3cm}
|>{\columncolor[HTML]{FFF2CC}}p{4cm}
|>{\columncolor[HTML]{EA9999}}p{3cm}|}
\cline{1-3}
\textbf{Krav}  & \textbf{Mått}                                                     & \textbf{Beskrivning}                                                                 & \textbf{Kravtyp} \\ \cline{1-3}
First thing    & \qtyrange{50}{54}{\celsius}                                       & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Second thing   & \qty[parse-numbers=false]{x}{\milli\ampere\hour} (\qty{5}{\volt}) & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Third thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Fourth thing   & \qty{>100}{\celsius}                                              & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Fifth thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Sixth thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål         \\
Seventh thing  & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål         \\
Eighth thing   & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål                    
\end{tabular}
\end{table}


\end{document}

结果

您不希望最后一列出现水平线,有什么原因吗?

答案2

如果您打算创建更复杂的表格,请考虑使用tblr来自的环境tabularray。它的界面非常简化。例如,我已将行和列的交替颜色添加到您的表格中。

您可以定义全局颜色并在整个表格中使用带有色调的名称,而不是在单元格中重复绝对值。

如果中间列包含较长的文本,则在较宽的列中可能会看起来更好。此外,请考虑加载microtype包以改善较窄环境中的文本。

以下是略作改动的表格:

在此处输入图片描述

\documentclass[12pt,a4paper,swedish]{report}
\usepackage[svgnames]{xcolor} % For tables (color)
\usepackage{amssymb,amsmath}                        % Mathematical expressions (American mathematical society)
\usepackage[
  top=3cm,
  bottom=3cm,
  inner=3cm,
  outer=3cm,
]{geometry}                                         % Page margin lengths           
\usepackage{microtype}
\usepackage{siunitx}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
% \usepackage{textcomp}                               % Fonts, symbols etc.
% \usepackage{moreverb}                               % List settings
% \usepackage{graphicx}                               % Figures
% \usepackage[T1]{fontenc}                            % Output settings
% \usepackage[utf8]{inputenc}                         % Input settings
% \usepackage{float}                                  % Enables object position enforcement using [H]
% \usepackage{gensymb}                                % For certain symbols such as \degree
% \usepackage{parskip}                                % Enables vertical spaces correctly 

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables

\colorlet{kravmcol}{BlueViolet!80}
\colorlet{mattcol}{Blue!50}
\colorlet{beskcol}{red!20!yellow!60}
\colorlet{kravtcol}{LightCoral}

\sisetup{
  range-units=single,
  range-phrase={-},
}


\begin{document}
\begin{table}[tbh]
  \footnotesize
  \renewcommand{\arraystretch}{1.5}
  \begin{tblr}{
      colspec = {*2{X[l]} X[3,l] X[l]},
      row{1} = {font=\bfseries, halign=c},
      hline{1,Z} = {wd=0.8pt},
      hline{2} = {wd=0.5pt},
      hline{3-Y} = {wd=0.2pt},
      cell{1}{1} = {bg=kravmcol!80},
      cell{1}{2} = {bg=mattcol!80},
      cell{1}{3} = {bg=beskcol!80},
      cell{1}{4} = {bg=kravtcol!80},
      cell{even[2-Z]}{1} = {bg=kravmcol!50}, cell{odd[2-Z]}{1} = {bg=kravmcol!30},
      cell{even[2-Z]}{2} = {bg=mattcol!50},  cell{odd[2-Z]}{2} = {bg=mattcol!30},
      cell{even[2-Z]}{3} = {bg=beskcol!50},  cell{odd[2-Z]}{3} = {bg=beskcol!30},
      cell{even[2-Z]}{4} = {bg=kravtcol!50}, cell{odd[2-Z]}{4} = {bg=kravtcol!30},
    }
    Krav          & Mått
    & Beskrivning                                                                          & Kravtyp \\
    First thing   & $\qtyrange{50}{54}{\celsius}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Second thing  & $\mathop{x} \unit{\mA}(5)\unit{\hour}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Third thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Fourth thing  & $> 100\unit{\celsius}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Fifth thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Sixth thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål \\
    Seventh thing & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål \\
    Eighth thing  & -
    & This is how the thing works and so on. These cells have much text. Kind of like      & Önskemål \\
  \end{tblr}
\end{table}
\end{document}

相关内容