我正在使用该enumitem
包来定义一个合法列表:
\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\setlist[legal]{label*=\arabic*.}
列表的输出如下所示:
1. foo
1.1 bar
1.1.1 bla
1.2 baz
1.2.1 blubb
2. fuuu
但是,如果项目很多且嵌套很深,那么这种方法就会有些麻烦。因此,我希望所有标签都对齐到最左侧,但保留项目本身的缩进,如下所示:
1. foo
1.1 bar
1.1.1 bla
1.2 baz
1.2.1 blubb
2. fuuu
我想这会让在列表中搜索条目变得更容易。关于如何实现这一点,您有什么想法吗?
我已经尝试过align
该enumitem
包的选项,但是没有达到预期的效果。
答案1
这是一个没有任何包的解决方案,可以保证对于每个计数器值 < 100 的项目文本缩进都会被保留(如果任何计数器超过 9,则另一个解决方案会出现不良行为)。
编辑:这个新版本允许嵌套最多六级,并纠正了第一个版本的一些间距问题:
\documentclass{article}
\usepackage{lipsum}
\newlength\labwd
\makeatletter
\newcommand\labelenumv{}
\newcommand\labelenumvi{}
\newcounter{enumv}[enumiv]
\newcounter{enumvi}[enumv]
\newenvironment{myenum}
{\advance\@enumdepth\@ne
\ifnum \@enumdepth >6\@toodeep\else
\edef\@enumctr{enum\romannumeral\the\@enumdepth}
\renewcommand\labelenumi{\arabic{enumi}.}
\renewcommand\labelenumii{\labelenumi\arabic{enumii}.}
\renewcommand\labelenumiii{\labelenumii\arabic{enumiii}.}
\renewcommand\labelenumiv{\labelenumiii\arabic{enumiv}.}
\renewcommand\labelenumv{\labelenumiv\arabic{enumv}.}
\renewcommand\labelenumvi{\labelenumv\arabic{enumvi}.}
\renewcommand\theenumii{\arabic{enumii}}
\renewcommand\theenumiii{\arabic{enumiii}}
\renewcommand\theenumiv{\arabic{enumiv}}
\renewcommand\p@enumii{\theenumi.}
\renewcommand\p@enumiii{\theenumi.\theenumii.}
\renewcommand\p@enumiv{\theenumi.\theenumii.\theenumiii.}
\renewcommand\p@enumv{\theenumi.\theenumii.\theenumiii.\theenumiv.}
\renewcommand\p@enumvi{\theenumi.\theenumii.\theenumiii.\theenumiv.\theenumv.}
\begin{list}{\csname label\@enumctr\endcsname}{%
\usecounter\@enumctr
\setlength\labelwidth{0pt}
\setlength\labelsep{0pt}
\setlength\leftmargin{15pt}
\setlength\labwd{\ifcase\@enumdepth\or -15pt\or -30pt\or -45pt\or -60pt\or -75pt\or -90pt\fi}%
\renewcommand\makelabel[1]{\hspace*{\labwd}##1\hfill}%
}%
\fi%
}
{\ifnum \@enumdepth >6\else\end{list}\fi}
\makeatother
\begin{document}
\lipsum[2]
\begin{myenum}\ignorespaces
\item\label{a} First item.
\begin{myenum}\ignorespaces
\item\label{b} First subitem.
\begin{myenum}\ignorespaces
\item\label{c} First subsubitem.
\item Second subsubitem.
\begin{myenum}
\item\label{d} First subsubsubitem.
\item Second subsubsubitem.
\begin{myenum}
\item\label{e} First subsubsubsubitem.
\item \lipsum[1]
\end{myenum}
\end{myenum}
\item Third subsubitem.
\end{myenum}
\item Second subitem.
\end{myenum}
\item Second item.
\end{myenum}
\begin{myenum}
\setcounter{enumi}{8}
\item First item.
\begin{myenum}
\setcounter{enumii}{8}
\item First subitem.
\begin{myenum}
\setcounter{enumiii}{8}
\item First subsubitem.
\item Second subsubitem.
\begin{myenum}
\setcounter{enumiv}{8}
\item First subsubsubitem.
\item Second subsubsubitem.
\end{myenum}
\item Third subsubitem.
\end{myenum}
\item Second subitem.
\end{myenum}
\item Second item.
\end{myenum}
\end{document}
答案2
您可以使用leftmargin=0pt, labelwidth=!
这意味着enumitem
将计算标签的宽度以适应 0pt 的左边距。结合可得到align=left
所需的结果。
\setlist[legal]{label*=\arabic*., leftmargin=0pt, labelsep=1.5em, align=left, labelwidth=!}
设置labelsep
不是严格要求的,但我发现 1.5em 比默认值更合适。
答案3
在这里,我尝试修改@GonzaloMedina 的代码以允许更深的嵌套并解决错误处理的问题\@toodeep
(由于太长而未添加为注释)。
\documentclass{article}
\usepackage{lipsum}
\newlength\labwd
\makeatletter
\newcommand\labelenumv{}
\newcommand\labelenumvi{}
\newcounter{enumv}[enumiv]
\newcounter{enumvi}[enumv]
\newenvironment{myenum}
{\advance\@enumdepth\@ne
\ifnum \@enumdepth >6\@toodeep\else
\edef\@enumctr{enum\romannumeral\the\@enumdepth}
\renewcommand\labelenumi{\arabic{enumi}.}
\renewcommand\labelenumii{\labelenumi\arabic{enumii}.}
\renewcommand\labelenumiii{\labelenumii\arabic{enumiii}.}
\renewcommand\labelenumiv{\labelenumiii\arabic{enumiv}.}
\renewcommand\labelenumv{\labelenumiv\arabic{enumv}.}
\renewcommand\labelenumvi{\labelenumv\arabic{enumvi}.}
\begin{list}{\csname label\@enumctr\endcsname}{%
\usecounter\@enumctr
\setlength\labelwidth{5pt}
\setlength\labelsep{\z@}
\setlength\leftmargin{15pt}
\setlength\itemindent{5pt}
\setlength\labwd{\ifcase \@enumdepth \or -15pt\or -30pt\or -45pt\or -60pt\or -75pt\or -90pt\fi}
\renewcommand\makelabel[1]{\hspace*{\labwd}##1\hfill}
}%
\fi
}
{\ifnum \@enumdepth >6\else\end{list}\fi}
\makeatother
\begin{document}
\lipsum[2]
\begin{myenum}
\item First item.
\begin{myenum}
\item First subitem.
\begin{myenum}
\item First subsubitem.
\item Second subsubitem.
\begin{myenum}
\item First subsubsubitem.
\item Second subsubsubitem.
\begin{myenum}
\item First subsubsubsubitem.
\item \lipsum[1]
\end{myenum}
\end{myenum}
\item Third subsubitem.
\end{myenum}
\item Second subitem.
\end{myenum}
\item Second item.
\end{myenum}
\begin{myenum}
\setcounter{enumi}{8}
\item First item.
\begin{myenum}
\setcounter{enumii}{8}
\item First subitem.
\begin{myenum}
\setcounter{enumiii}{8}
\item First subsubitem.
\item Second subsubitem.
\begin{myenum}
\setcounter{enumiv}{8}
\item First subsubsubitem.
\item Second subsubsubitem.
\end{myenum}
\item Third subsubitem.
\end{myenum}
\item Second subitem.
\end{myenum}
\item Second item.
\end{myenum}
\end{document}
一个悬而未决的问题是处理多行项中的缩进 - 目前,此类项第一行的缩进大约太深一个字符 - 并且可能有更好的方法来编写解决此问题的黑客程序(我是 LaTeX 编程新手)。
欢迎提出建议。