I'm trying to style itemize
lists that appear in tabular environments (specifically in a longtable
). Ideally, I'd like to remove the left margin from lists that appear in tables while maintaining the margin on lists outside of tables.
There are several answers here already about how to use the enumitem
package to change the margins of lists: you can set leftmargin=*
on specific lists, or you can create a new custom itemize
environment and use it specifically inside tables.
However, I am using pandoc to convert Markdown into TeX, so I have no control over the resulting TeX output—I can't add \begin{itemize}[leftmargin=*]
or use a custom \begin{marginlessitemize}
. enumitem
has a \setlist
macro that lets you set list settings globally, and \setlist[itemize]{leftmargin=*}
removes the left margin from all lists, which isn't ideal, since I'm only trying to target lists inside tables.
Is there a way to apply \setlist{...}
only to itemize environments that are nested inside tabular environments? In a perfect world, it'd be cool to use some sort of logic in the preamble: if itemize
is in a table, use no margin; otherwise use a margin.
Here's a MWE…
(The messy longtable
output comes from this Markdown table, which pandoc converts into TeX):
+-----------+-----------+
| Thing | List |
+===========+===========+
| Thing 1 | - Item 1 |
| | - Item 2 |
+-----------+-----------+
| Thing 2 | - Item 3 |
| | - Item 4 |
+-----------+-----------+
This TeX file…
\documentclass[11pt,article,oneside]{memoir}
\usepackage{longtable}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\begin{itemize}
\tightlist
\item
Item 1
\item
Item 2
\end{itemize}
\begin{longtable}[]{@{}ll@{}}
\caption{This is a table.}\tabularnewline
\toprule
\begin{minipage}[b]{0.21\columnwidth}\raggedright
Thing
\end{minipage} & \begin{minipage}[b]{0.27\columnwidth}\raggedright
List
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.21\columnwidth}\raggedright
Thing
\end{minipage} & \begin{minipage}[b]{0.27\columnwidth}\raggedright
List
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.21\columnwidth}\raggedright
Thing 1
\end{minipage} & \begin{minipage}[t]{0.27\columnwidth}\raggedright
\begin{itemize}
\tightlist
\item
Item 1
\item
Item 2
\end{itemize}
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
Thing 2
\end{minipage} & \begin{minipage}[t]{0.27\columnwidth}\raggedright
\begin{itemize}
\tightlist
\item
Item 3
\item
Item 4
\end{itemize}
\end{minipage}\tabularnewline
\bottomrule
\end{longtable}
\end{document}
…generates this PDF:
If I add this to the preamble…
\usepackage{enumitem}
\setlist[itemize]{leftmargin=*}
…the left margin in the list inside the table disappears, but it also disappears in the list outside the table:
If I create a custom itemize environment, I can use it in the table:
\newlist{marginlessitemize}{itemize}{1}
\setlist[marginlessitemize]{label=\textbullet,leftmargin=*}
...
% Inside a table cell...
\begin{marginlessitemize}
\item
Item 1
\item
Item 2
\end{marginlessitemize}
That works, but because the TeX is generated automatically, I can't use the custom environment without manually editing the converted file. Hence the need to somehow automatically set no margin on lists inside tables.
Best Answer
The following works (just for
longtable
) if you place it in your preamble:Complete MWE:
For a short explanation: The start of the
longtable
-environment is defined as:So after evaluating some stuff it calls the
\LT@array
macro in which we inject our code which alters theitemize
-environment and because it is inside of a\begingroup...\endgroup
construct the changes made are local.EDIT: To improve the vertical spacing I added
after=\strut
to the\setlist
.