表格与有或无 pardent 的列表看起来相同

表格与有或无 pardent 的列表看起来相同

我想定义一个tabular与我的列表(绿色标签)环境完全相同的(黄色标签)类型环境。我可以让它们接近,但无法让它们完全相同。如果我不包含该parskip包,那么下面的 MWE 会产生截然不同的结果。

有了这个parskip包,我已经接近目标了,但还没有完全实现:

在此处输入图片描述

代码:

\documentclass{article}
%\usepackage[parfill]{parskip}% <--- should work with or without this package.
\usepackage{showframe}
\usepackage{array}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{tikz}

%% http://tex.stackexchange.com/questions/64982/why-is-parskip-zero-inside-a-minipage
\newlength{\currentparskip}

\newcommand*{\CircledText}[2][fill=green]{%
\tikz[baseline=(char.base)]{%
    \node[
        shape=circle,
        draw=gray, thick, 
        fill opacity=0.3,
        text opacity=1,
        inner sep=0.8pt,
        outer sep=0pt,
        #1
    ] (char) {#2};}%
}%

%% --------------------------------- List Version
\newlist{MyExample}{enumerate}{2}%
\setlist[MyExample,1]{label=\protect\CircledText{\arabic*}}

\newcommand*{\MyList}{%
\par\noindent
\begin{MyExample}
    \item First list item
    \item Second list item
\end{MyExample}
}%


%% --------------------------------- Tabular Version
\newenvironment{MyExampleTabular}[2][]{%
    \def\item{\stepcounter{MyExamplei}\CircledText[fill=yellow]{\arabic{MyExamplei}}}%
    \renewcommand*{\arraystretch}{1.7}%
    \hspace*{\dimexpr\leftmargin-\labelwidth\relax}% <-- close
    %\hspace*{\labelsep}% <-- close
    \tabular[#1]{#2}%
}{%
    \endtabular%
}%

\newcommand*{\MyTabular}{%
\par\noindent
\begin{MyExampleTabular}{@{}l@{}>{\hspace{\labelsep}} l}
    \item & First list item  \\
    \item & Second list item \\
\end{MyExampleTabular}
}%


\begin{document}

\textbf{Horizontal Alignment: Outside Minipage}

\MyList
\MyTabular


\textbf{Horizontal Alignment: Inside Minipage}

\setlength{\currentparskip}{\parskip}% save the value
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
    \MyTabular
\end{minipage}%

\bigskip
\textbf{Vertical Alignment}

\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
\end{minipage}%
% ----------------------------
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyTabular
\end{minipage}%
\end{document}

答案1

您可以尝试 listliketab 包:

\documentclass{article}
\usepackage[parfill]{parskip}% <--- should work with or without this package.
\usepackage{showframe}
\usepackage{array}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{tikz}

%% http://tex.stackexchange.com/questions/64982/why-is-parskip-zero-inside-a-minipage
\newlength{\currentparskip}
\setlength{\currentparskip}{\parskip}

\newcommand*{\CircledText}[2][fill=green]{%
\tikz[baseline=(char.base)]{%
    \node[
        shape=circle,
        draw=gray, thick,
        fill opacity=0.3,
        text opacity=1,
        inner sep=0.8pt,
        outer sep=0pt,
        #1
    ] (char) {#2};}%
}%

%% --------------------------------- List Version

\newlist{MyExample}{enumerate}{2}%
\setlist[MyExample,1]{parsep=\currentparskip,label=\protect\CircledText{\arabic*}}

\newcommand*{\MyList}{%
 \begin{MyExample}
    \item First list item
    \item Second list item
 \end{MyExample}}%

\usepackage{listliketab}
\storestyleof{MyExample}

\newcommand\MyTabular{%
\begin{listliketab}
\def\item{\stepcounter{MyExamplei}\CircledText[fill=yellow]{\arabic{MyExamplei}}}%
\begin{tabular}{Ll}
    \item& First list item  \\
    \item& Second list item \\
\end{tabular}
\end{listliketab}}



\begin{document}


\textbf{Horizontal Alignment: Outside Minipage}

\MyList
\MyTabular


\textbf{Horizontal Alignment: Inside Minipage}

\setlength{\currentparskip}{\parskip}% save the value
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
    \MyTabular
\end{minipage}%

\bigskip
\textbf{Vertical Alignment}

\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
\end{minipage}%
% ----------------------------
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyTabular
\end{minipage}%
\end{document}

parskip 的结果

在此处输入图片描述

未使用 parskip 的结果

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass{article}
%\usepackage[parfill]{parskip}% <--- should work with or without this package.
\usepackage{showframe}
\usepackage{array}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{tikz}

%% http://tex.stackexchange.com/questions/64982/why-is-parskip-zero-inside-a-minipage
\newlength{\currentparskip}

\newcommand*{\CircledText}[2][fill=green]{%
\tikz[baseline=(char.base)]{%
    \node[
        shape=circle,
        draw=gray, thick, 
        fill opacity=0.3,
        text opacity=1,
        inner sep=0.8pt,
        outer sep=0pt,
        #1
    ] (char) {#2};}%
}%

%% --------------------------------- List Version
\newlist{MyExample}{enumerate}{2}%
\setlist[MyExample,1]{label=\protect\CircledText{\arabic*}}

\newcommand*{\MyList}{%
%\par\noindent% boo
\begin{MyExample}
    \item First list item
    \item Second list item
\end{MyExample}
}%


%% --------------------------------- Tabular Version
\newenvironment{MyExampleTabular}[2][]{%
    \def\item{\refstepcounter{MyExamplei}\CircledText[fill=yellow]{\arabic{MyExamplei}}}%
    \renewcommand*{\arraystretch}{1.7}%
    \hspace*{\dimexpr\labelsep+\itemindent+\leftmargin-\labelwidth\relax}% <-- close
    %\hspace*{\labelsep}% <-- close
    \tabular[#1]{#2}%
}{%
    \endtabular%
}%

\newcommand*{\MyTabular}{%
\par\noindent
\begin{MyExampleTabular}[t]{@{}l@{}>{\hspace{\labelsep}} l}
    \item & First list item  \\
    \item & Second list item \\
\end{MyExampleTabular}
}%


\begin{document}

\textbf{Horizontal Alignment: Outside Minipage}

\MyList
\MyTabular


\textbf{Horizontal Alignment: Inside Minipage}

\setlength{\currentparskip}{\parskip}% save the value
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
    \MyTabular
\end{minipage}%

\bigskip
\textbf{Vertical Alignment}

\begin{minipage}[t]{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
\end{minipage}%
% ----------------------------
\begin{minipage}[t]{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyTabular
\end{minipage}%
\end{document}

注意我在这里使用[t]在顶部获得正确的垂直对齐,但这将丢失有关最后一项基线的信息,从而使末端的垂直空间变得更加困难(请参阅 l3 棺材的文档)

答案3

下面的方法似乎奏效了。无论有没有 ,两者之间的间距似乎都是一致的parskip。我冒昧地添加了一些项目以供比较。

\documentclass{article}
%\usepackage[parfill]{parskip}% <--- should work with or without this package.
\usepackage{showframe}
\usepackage{array}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{tikz}

%%Added in Edit to make the explicit space implicit
\makeatletter
    \newlength{\arrayexplvsep}
    \setlength{\arrayexplvsep}{0pt}
    \def\new@argarraycr[#1]{\@argarraycr[\dimexpr#1+\arrayexplvsep\relax]}
    \def\@xarraycr{\@ifnextchar [\new@argarraycr{\new@argarraycr[0pt]}}
\makeatother

%% http://tex.stackexchange.com/questions/64982/why-is-parskip-zero-inside-a-minipage
\newlength{\currentparskip}

\newcommand*{\CircledText}[2][fill=green]{%
\smash{\tikz[baseline=(char.base)]{%<=====added \smash so that different numerals do not impact line height
    \node[
        shape=circle,
        draw=gray, thick, 
        fill opacity=0.3,
        text opacity=1,
        inner sep=0.8pt,
        outer sep=0pt,
        #1
    ] (char) {#2};}}\strut%<=====added strut for consistent height
}%

%% --------------------------------- List Version
\newlist{MyExample}{enumerate}{2}%
\setlist[MyExample,1]{label=\protect\CircledText{\arabic*}}

\newcommand*{\MyList}{%
\par\noindent
\begin{MyExample}
    \item First list item
    \item Second list item
    \item Third list item
    \item Fourth list item
    \item Fifth list item
\end{MyExample}
}%


%% --------------------------------- Tabular Version
\newenvironment{MyExampleTabular}[2][]{%
    \def\item{\hfill\refstepcounter{MyExamplei}\CircledText[fill=yellow]{\arabic{MyExamplei}}}%<=====added \hfill to make it right aligned (default); also changed to \refstepcounter
    \unskip\vspace{\topsep}\par\noindent%<=====added the \unskip, \topsep space, and moved \par\noindent
    \setlength{\arrayexplvsep}{\dimexpr\itemsep+\parsep\relax}%<=====added in Edit to define the explicit space
    \tabular[#1]{#2}%
}{%
    \endtabular%
}%

\newcommand*{\MyTabular}{%
\begin{MyExampleTabular}[t]{@{}p{\dimexpr\leftmargin-\labelsep\relax}@{}>{\hspace{\labelsep}}l}%%<=====added [t] and changed l to p{\dimexpr\leftmargin-\labelsep\relax}
    \item & First list item\\
    \item & Second list item\\
    \item & Third list item\\
    \item & Fourth list item\\
    \item & Fifth list item%\\%<====inclusion will add space below, which throws off vertical alignment when minipage does not use [t] option.
\end{MyExampleTabular}
}%


\begin{document}

\textbf{Horizontal Alignment: Outside Minipage}

\MyList
\MyTabular


\textbf{Horizontal Alignment: Inside Minipage}

\setlength{\currentparskip}{\parskip}% save the value
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
    \MyTabular
\end{minipage}%

\bigskip
\textbf{Vertical Alignment}

\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyList
\end{minipage}%
% ----------------------------
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{\currentparskip}% restore the value
    \MyTabular
\end{minipage}%
\end{document}

垂直对齐的修改:

  1. 在定义中添加\smash和,以便标签具有一致的高度,而不管包含的文本如何(否则,垂直对齐就会被丢弃)。\strut\CircledText
  2. 删除\renewcommand*{\arraystretch}{1.7}并添加与( )一致的每个项目后的明确间距。enumitem\\[\dimexpr\itemsep+\parsep\relax] 编辑:根据 OP 的要求,通过在序言中重新定义 并在 中添加 ,显\\式空间已变为隐式(自动包含在每个 中)。进行定义是为了通过典型机制将显式空间添加到 定义的隐式中。array\@xarraycr\setlength{\arrayexplvsep}{\dimexpr\itemsep+\parsep\relax}MyExampleTabulararrayexplvsep[]
  3. 移入\par\noindent环境MyExampleTabular并添加\vspace{\topsep}以保持一致的顶部间距enumitem\unskip添加了以使前两个测试中的列表之间的垂直间距保持一致。

水平对齐的修改(参见手册第 3-5 页enumitem):

  1. 添加到的\hfill重新定义以与中的默认标签对齐保持一致。\itemMyExampleTabularrightenumitem
  2. lp{\dimexpr\leftmargin-\labelsep\relax}in替换以模仿中的\MyTabular默认标签对齐方式。rightenumitem

没有parskip

未使用 parskip 的结果

parskip

parskip 的结果

相关内容