LaTex-Table(长文本自动换行)

LaTex-Table(长文本自动换行)

我需要在此表中进行哪些更改,以便长文本(“Anmerkungen”)可以适合(合并的)单元格(自动换行)? 它在上一行中有效,

\usepackage{xcolor}
\usepackage{tabularray}
\usepackage[export]{adjustbox}
\definecolor{grau}{RGB}{229,229,229}
\begin{textblock}{285}(5,25)    
    \begin{table}[h]
        \centering
        \begin{tblr}{           
                width=1\linewidth,
                colspec={|X[1,1,halign=c]|X[2,2,halign=c]|X[3,2,halign=c]|X[4,2,halign=c]|},        
                cell{3}{1} = {valign=m},
                row{1} = {grau,halign=c},   
                column{1} = {grau},                 
                }
                \hline
                \multicolumn{4}{|c|}{\textbf{TITLE}} \\
                \hline
                \multirow{2}{*}{aktuelles Quartal}
                \hline
                  & A & B & C \\
                \hline
                 &  Grafik  & Grafik & Grafik \\
                \hline
                Zeitverlauf &  Grafik & it works it works it works it works it works it works it works it works it works it works it works it works it works   & Grafik \\
                \hline          
                Anmerkungen & \multicolumn{3}{|c|}{it doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work} \\                           
                \hline              
                \multicolumn{4}{|c|}{\textbf{}} \\              
                \hline
        \end{tblr}
    \end{table}
\end{textblock} 

答案1

我猜你正在寻找这样的东西:

在此处输入图片描述

  • 在您的表格代码中,您有一堆“经典”表格包的语法(例如tabularx和其他),并且tabularray将来会意识到不再容忍这种混合。我强烈建议您仅使用tabularray语法
  • 在“Anmerkungen”行中,您使用\multicolumn{3}{c}{...}不允许将其包含分解为多行文本的功能。
  • 相反,您应该使用\SetCell[c=3]{<width>}宽度等于其跨越列之和的位置。

可能的解决方案是:

\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
\definecolor{grau}{RGB}{229,229,229}
%\begin{textblock}{285}(5,25)
    \begin{table}[h]
        \centering
        \begin{tblr}{
                width=1\linewidth,
                hlines, vlines,
                colspec={ X[1.5,c,m] X[c] X[2,j,m] X[2,j,m] },
                cell{3}{1} = {m},
                row{1} = {font=\bfseries, bg=grau,c},
                column{1} = {bg=grau},
                }
                \SetCell[c=4]{c}    TITLE   
                    &  &   &   \\
                \SetCell[r=2]{c}    aktuelles Quartal
                    & A & B & C \\
                    &  \includegraphics[width=\linewidth,valign=c]{example-image-duck}   
                        & Grafik 2 
                            & Grafik 3 \\
                Zeitverlauf 
                    &  \includegraphics[width=\linewidth,valign=c]{example-image-duck}
                        & it works it works it works it works it works it works it works it works it works it works it works it works it works
                            & Grafik 6 \\
                Anmerkungen 
                    & \SetCell[c=3]{j,m,0.75\linewidth} it doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work
                        &   &   \\
                \SetCell[c=4]{c} 
                    &   &   &   \\
        \end{tblr}
    \end{table}
%\end{textblock} 
\end{document}

附录:
使用\adjincludegraphics˙ defined inadjustbox 包,插入图像的代码会稍微短一些:

\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{xcolor}
\definecolor{grau}{RGB}{229,229,229}
\usepackage{tabularray}

\begin{document}
    \begin{table}[h]
\adjustboxset{width=\linewidth,
              valign=m}
\begin{tblr}{hlines, vlines,
             colspec = {X[1.5,c,m, bg=grau] 
                        X[c] X[2,j,m] X[2,j,m]},
             cell{1}{1} = {c=4}{font=\bfseries, bg=grau, c},
             hspan=minimal
             }
    TITLE
        &  &   &   \\
    \SetCell[r=2]{c}    aktuelles Quartal
        & A & B & C \\
        &  \adjincludegraphics{example-image-duck}
            & Grafik 2
                & Grafik 3 \\
    Zeitverlauf
        &  \adjincludegraphics{example-image-duck}
            & it works it works it works it works it works it works it works it works it works it works it works it works it works
                & Grafik 6 \\
    Anmerkungen
        & \SetCell[c=3]{j,m} now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works now it works
        &   &   \\
\end{tblr}
   \end{table}
\end{document}

结果和以前一样。

答案2

通过和的组合\SetCellhspan=minimal无需手动确定跨越单元格所需的宽度:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tabularray}
\usepackage{geometry}
\usepackage{xcolor}
\begin{document}

\definecolor{grau}{RGB}{229,229,229}    
    \begin{table}[h]
        \centering
        \begin{tblr}{           
                width=1\linewidth,
                colspec={|X[1,1,halign=c]|X[2,2,halign=c]|X[3,2,halign=c]|X[4,2,halign=c]|},        
                cell{3}{1} = {valign=m},
                row{1} = {grau,halign=c},   
                column{1} = {grau},
                hspan=minimal         %<--------------- added        
                }
                \hline
                \multicolumn{4}{|c|}{\textbf{TITLE}} \\
                \hline
                \multirow{2}{*}{aktuelles Quartal}
                \hline
                  & A & B & C \\
                \hline
                 &  Grafik  & Grafik & Grafik \\
                \hline
                Zeitverlauf &  Grafik & it works it works it works it works it works it works it works it works it works it works it works it works it works   & Grafik \\
                \hline          
                Anmerkungen & \SetCell[c=3]{j}it doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work it doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt workit doesnt work \\                           % <-------------- \SetCell instead of \multicolumn 
                \hline              
                \multicolumn{4}{|c|}{\textbf{}} \\              
                \hline
        \end{tblr}
    \end{table}

\end{document}

相关内容