6

I would like to define a tabular (yellow labels) type environment that looks identical to my list (green labels) environment. I can get them close but have not been able to get them exactly alike. If I don't include the parskip package then the the MWE below produce drastically different results.

With the parskip package I am close but not quite there:

enter image description here

Code:

\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}

Peter Grill
  • 223,288
  • I suppose you are missing \itemindent, to move your tabular the last little bit to the right, as per this answer. I am curious, what are you trying to accomplish? Why not use an itemize environment, if you are looking for an itemize environment? AFAIK people tend to mutilate array-environments rather than tabular environments. You don't get \arraystretch without the array-package anyways, so you are depending on array already. Your tabular is an array already, I think. – Bananguin Jul 28 '16 at 08:08
  • @user1129682 \arraystretch is defined in the latex format not a package. The only difference between array and tabular is array cells default to math mode, and I see no math here? – David Carlisle Jul 28 '16 at 08:23
  • @user1129682: A particular section is a list, but a few times it is a list of equations where I want to align things. Have been using a \makebox, etc to get the alignment correct, so would prefer to just be able to use a tabular environment that has the same spacing for those few cases. – Peter Grill Aug 01 '16 at 03:58
  • @PeterGrill: I see. Have you had a chance to look into special packages for math/formulae? There is a considerable amount of related work. Maybe, if you add an example of the actual use case, somebody may be able to point you towards the correct package and method. – Bananguin Aug 01 '16 at 10:03
  • Does the tabular require multi-line (auto-breaking or manual breaking) cells or is each cell entry a single line? – Steven B. Segletes Aug 03 '16 at 17:50
  • @StevenB.Segletes: I do not need it to do multipe lines. – Peter Grill Aug 04 '16 at 06:40

3 Answers3

5

You can try the listliketab package:

\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}

Result with parskip

enter image description here

Result without parskip

enter image description here

Ulrike Fischer
  • 327,261
3

enter image description here

\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}

Note I've used [t] here to get vertical alignment correct at the top, but this will lose information about the baseline of the last item which makes vertical space at the end harder (see the documentation of l3 coffins)

David Carlisle
  • 757,742
2

The following seems to do the trick. The spacing appears to be consistent between the two both with and without parskip. I took the liberty of adding a few more items for comparison.

\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}

Modifications for vertical alignment:

  1. Added \smash and \strut to the \CircledText definition so that the label was a consistent height, regardless of the contained text (otherwise, the vertical alignment is thrown off).
  2. Removed \renewcommand*{\arraystretch}{1.7} and added explicit spacing after each item consistent with enumitem (\\[\dimexpr\itemsep+\parsep\relax]). Edit: As per the OP's request, the explicit space has been made implicit (automatically included with each \\) through a redefinition of array's \@xarraycr in the preamble and the addition of \setlength{\arrayexplvsep}{\dimexpr\itemsep+\parsep\relax} in MyExampleTabular. The definition was made so that explicit space could be added to the implicit defined by arrayexplvsep through the typical [] mechanism.
  3. Moved \par\noindent into the MyExampleTabular environment and added \vspace{\topsep} for consistent top spacing with enumitem. \unskip was added to make the vertical spacing between the lists consistent in the first two tests.

Modifications for horizontal alignment (see pages 3-5 of the enumitem manual):

  1. Added \hfill to the redefinition of \item in MyExampleTabular for consistency with the default right label alignment in enumitem.
  2. Replaced l with p{\dimexpr\leftmargin-\labelsep\relax} in \MyTabular to mimic the default right label alignment in enumitem.

Without parskip:

Result without parskip

With parskip:

Result with parskip

Guho
  • 6,115
  • Results look good. But, don't quite like the manual tweaking that is ncessary for each line of the MyExampleTabular environment. There must be a way to package the manul vertical spacing into the definition of the environment itself. – Peter Grill Aug 02 '16 at 01:43
  • @PeterGrill Agreed. Answer edited to automate it. – Guho Aug 02 '16 at 20:30