将 tblr 表与 itemize 结合起来 - 失败

将 tblr 表与 itemize 结合起来 - 失败

在以下示例中,我尝试将 tblr 表与 itemize 组合在一起。但是失败了:

! LaTeX Error: \begin{tblr} on input line 12 ended by \end{itemize}.

这是代码示例:

\documentclass{scrbook}
\usepackage{tabularray}

\begin{document}    
\begin{center}
% Style changes
\small\centering\renewcommand{\arraystretch}{1.4}  
% tabular
\begin{tblr}{width=0.9\textwidth, colspec={|X|X|X|}}
    \hline
header X &
header items (X) &   
header enums (X) \\
    \hline
    %
    This is a long text. This is a long text. This is a long text.
    &
    \begin{itemize}
        \item This is a long text.
        \item This is a long text.
        \item This is a long text.
    \end{itemize}
    &
    \begin{enumerate}
        \item This is a long text.
        \item This is a long text.
        \item This is a long text.
    \end{enumerate}
    \\ \hline
\end{tblr}
\end{center}
\end{document}

答案1

这是一个解决方案:

\documentclass{scrbook}
\usepackage{tabularray}
\usepackage{tabularray,varwidth,enumitem}

\begin{document}    
    \begin{center}
        % Style changes
        \small\centering\renewcommand{\arraystretch}{1.4}  
        % tabular
        \begin{tblr}{width=0.9\textwidth, colspec={|X|X|X|}}
            \hline
            header X &
            header items (X) &   
            header enums (X) \\
            \hline
            %
            This is a long text. This is a long text. This is a long text.
            &
        \begin{varwidth}[t]{\linewidth}
            \begin{itemize}[topsep=0pt,parsep=0pt,leftmargin=8pt]
                \item This is a long text.
                \item This is a long text.
                \item This is a long text. 
            \end{itemize}
        \end{varwidth}
            &
        \begin{varwidth}[t]{\linewidth}
            \begin{enumerate}[topsep=0pt,parsep=0pt,leftmargin=12pt]
                \item This is a long text.
                \item This is a long text.
                \item This is a long text.
            \end{enumerate}
        \end{varwidth}
            \\ \hline
        \end{tblr}
    \end{center}
\end{document}

输出

答案2

有时我发现尝试自己设计解决方案很有教育意义。下面是定义我自己的mynumeratemytemize尽可能简单的环境的粗略尝试。不可能嵌套它们,也不可能对它们做任何花哨的事情。唯一的目的就是有一个tblr包含仅有的一个列表。

此外,添加过多的\itemsmynumerate会导致标签与文本重叠,除非您编辑以 结尾的行% Longest expected number

笔记:每一行末尾的注释都很重要,不仅为了记录,而且为了防止出现意外的空格。

包含自定义环境的示例表

\documentclass{article}
\usepackage{tabularray}

% Warning: Don't nest these environments!
\newenvironment{mytemize}[1][\tiny$\bullet$]{%
    % Comments at line ends prevent unwanted whitespace
    \unskip% Ignore all spaces before \begin{}
    \begingroup% Don't leak \defs
    \newdimen\LabelWidth% Width of bullet plus space
    \settowidth{\LabelWidth}{#1}% Get label bullet
    \addtolength{\LabelWidth}{0.6ex}% Add some space to bullet
    \def\newparagraph{}% No new paragraph for first \item
    \def\item{%
        \newparagraph% New paragraph, if not first \item
        \def\newparagraph{\par}% Setup for second \item
        \hangindent=\LabelWidth% Indent following lines
        \parbox[c]{\LabelWidth}{#1}% Bullet. width=indent, vertically centered
    }%
    \ignorespacesafterend% Don't want spaces after \end
    \ignorespaces% Ignore all spaces before first \item
}{%
    \par% Makes sure that the last line also hangs,
    \endgroup% before \hangindent is reset by \endgroup.
}

\newenvironment{mynumerate}[1][\arabic]{%
    % Comments at line ends prevent unwanted whitespace
    \unskip% Ignore all spaces before \begin{}
    \begingroup% Don't leak \defs
    \newdimen\LabelWidth% Width of label plus space
    \setcounter{enumi}{8}% Longest expected number
    \settowidth{\LabelWidth}{#1{enumi}.}% Get label width
    \addtolength{\LabelWidth}{0.6ex}% Add some space to label
    \def\newparagraph{}% No new paragraph for first \item
    \setcounter{enumi}{0}% Item counter (first nesting level)
    \def\item{%
        \unskip% Ignore all spaces before \item
        \newparagraph% New paragraph, if not first \item
        \def\newparagraph{\par}% Setup for second \item
        \stepcounter{enumi}% Increment \item label
        \hangindent=\LabelWidth% Indent following lines
        \parbox[b]{\LabelWidth}{#1{enumi}.}% Number. width=indent, bottom aligned
    }%
    \ignorespacesafterend% Don't want spaces after \end
    \ignorespaces% Ignore all spaces before first \item
}{%
    \par% Makes sure that the last line also hangs,
    \endgroup% before \hangindent is reset by \endgroup.
}


\begin{document}    
\begin{center}
% Style changes
\small\centering\renewcommand{\arraystretch}{1.4}  
% tabular
\begin{tblr}{width=0.9\textwidth, colspec={|X|X|X|}}
    \hline
header X &
header items (X) &   
header enums (X) \\
    \hline
    %
    This is a long text. This is a long text. This is a long text.
    & 
    \begin{mytemize}
        \item This is a long text. This line breaks.
        \item This is a long text. This line breaks.
        \item This is a long text. This line breaks.
    \end{mytemize}
    &
    \begin{mynumerate}
        \item This is a long text. This line breaks.
        \item This is a long text. This line breaks.
        \item This is a long text. This line breaks.
    \end{mynumerate}
    \\ \hline
\end{tblr}
\end{center}
\end{document}

相关内容