根据艾伦的回答这里,我希望能够创建格式良好的“内联”枚举列表。
我遇到的问题是一行中的项目之间的间距总是固定的,这看起来不太好看。我正在寻找一种方法来平衡行之间的间距。任何关于如何实现美观的建议和帮助都将不胜感激。也许我不应该使用枚举环境?有问题的列表要么只是项目符号,要么是 a)-d)。
\documentclass[12pt]{article}
\usepackage[inline]{enumitem}
\makeatletter
% This command ignores the optional argument for itemize and enumerate lists
\newcommand{\inlineitem}[1][]{%
\ifnum\enit@type=\tw@
{\descriptionlabel{#1}}
\hspace{\labelsep}
\else
\ifnum\enit@type=\z@
\refstepcounter{\@listctr}\fi
\quad\@itemlabel\hspace{\labelsep}
\fi}
\makeatother
\begin{document}
% Combined inline list
\noindent This is a combined inline enumerated list.
\begin{itemize}
\item Less than 5 years \inlineitem Between 5 and 10 years
\item More than 10 years \inlineitem Do not know
\end{itemize}
\bigskip
\noindent Another list.
\begin{itemize}
\item £850
\inlineitem £1,000
\inlineitem £1,150
\inlineitem £1,500
\inlineitem Do not know
\end{itemize}
\bigskip
\noindent And the final one
\begin{enumerate}[label=(\alph*)]
\item Agree strongly \inlineitem Tend to agree \inlineitem Neither agree nor disagree
\item Tend to disagree \inlineitem Disagree strongly \inlineitem Don't know
\end{enumerate}
\end{document}
答案1
如果您知道每行需要多少项,那么这是一个可行的解决方案。您的 MWE 确实建议这样做。
\documentclass{article}
\newcounter{myitemcounter}
\newcommand{\myitemlabel}{$\bullet$\ }
\newcommand{\myitem}{%
\stepcounter{myitemcounter}
\myitemlabel
}
\newcommand{\anitem}[1]{%
\myitem #1 &
}
\newcommand{\lastitem}[1]{%
\myitem #1 \\
}
\newenvironment{inlineitemize}
{\setcounter{myitemcounter}{0}
\begin{tabular}{llllllllll} % you won't want more columns
}
{\end{tabular}}
\newenvironment{inlineenumerate}
{\setcounter{myitemcounter}{0}
\renewcommand{\myitemlabel}{(\alph{myitemcounter})\ }
\begin{tabular}{lllllllllll}
}
{\end{tabular}}
\begin{document}
\begin{inlineenumerate}
\anitem{Agree strongly}
\anitem{Tend to agree}
\lastitem{Neither agree nor disagree}
\anitem {Tend to disagree}
\anitem{Disagree strongly}
\lastitem{Don't know}
\end{inlineenumerate}
\begin{inlineitemize}
\anitem{Less than 5 years} \lastitem{Between 5 and 10 years}
\anitem{More than 10 years} \lastitem{Do not know}
\end{inlineitemize}
\end{document}
只需再做一些工作,您就可以将一个整数参数传递给新环境以指定列数,以便自动执行调用时间,lastitem
而不是anitem
。您还可以在环境中设置行间距tabular
以匹配您的 MWE。
答案2
不清楚您到底想改变什么空间。%
行尾缺少一些空间,导致输出中出现额外的空间。下面修复了这个问题,并用\labelsep
可以缩小 20% 或扩大 50% 的粘连替换了固定空间。不过,这些例子并没有太大的区别。
\newcommand{\inlineitem}[1][]{%
\ifnum\enit@type=\tw@
{\descriptionlabel{#1}}%
\hspace{1\labelsep\@plus .5\labelsep \@minus .2\labelsep}%
\else
\ifnum\enit@type=\z@
\refstepcounter{\@listctr}\fi
\quad \@itemlabel\hspace{1\labelsep\@plus .5\labelsep \@minus .2\labelsep }%
\fi}
答案3
作为使用 shortlst 包的解决方案,它是针对我给出的类似问题的答案的开发使用“enumitem”进行内联排列。可选参数是列表将按列数组织(默认为 3)。此外,如果项目太长而无法容纳在一列中,则会自动占用两列(或更多列):
\documentclass[a11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[textwidth=15cm, noheadfoot, nomarginpar]{geometry}
\geometry{showframe}
\usepackage{shortlst}
\newenvironment{tabenumerate}[1][3]{%
\settowidth{\labelwidth}{\labelenumi}%
\setlength{\leftmargini}{\dimexpr\parindent + \labelwidth + \labelsep\relax}%
\setlength{\shortitemwidth}{\dimexpr\linewidth/#1-\labelwidth-2\labelsep\relax}%
\begin{shortenumerate}}%
{\end{shortenumerate}}%
\newenvironment{tabitemize}[1][3]{%
\settowidth{\labelwidth}{\labelitemi}%
\setlength{\leftmargini}{\dimexpr\parindent + \labelwidth + \labelsep\relax}%
\setlength{\shortitemwidth}{\dimexpr\linewidth/#1-\labelwidth-2\labelsep\relax}%
\begin{shortitemize}}%
{\end{shortitemize}}%
\renewcommand{\labelenumi}{(\arabic{enumi})}
\parindent = 0pt
\begin{document}
This is an inline itemized list.
\begin{tabitemize}[2]
\item Less than 5 years \item Between 5 and 10 years
\item More than 10 years \item Do not know
\end{tabitemize}
Or
\begin{tabitemize}
\item Less than 5 years \item Between 5 and 10 years \item[]
\item More than 10 years \item Do not know
\end{tabitemize}
Another list.
\begin{tabitemize}[6]
\item £850
\item £1,000
\item £1,150
\item £1,500
\item Do not know
\end{tabitemize}
\bigskip
And the final one:
\begin{tabenumerate}
\item Agree strongly \item Tend to agree \item Neither agree nor disagree
\item Tend to disagree \item Disagree strongly \item Don't know
\end{tabenumerate}
\end{document}
答案4
我尝试使用hlist
具有可选列数的新环境来执行此操作。默认值为 2。不幸的是,每个项目都需要用括号括起来。\hitem{xyz.}
替换\item xyz.
它使用parbox
内部minipages
。远非完美。
\documentclass{article}
\newcounter{hitem}
\newenvironment{hlist}[2][2]{\def\hcol{#1}%
\begin{trivlist}\setcounter{hitem}{0}\item #2
}{\end{trivlist}}
\newcommand{\hitem}[1]{%
\raggedright\begin{minipage}[t]{\dimexpr\linewidth/\hcol-1em}
\refstepcounter{hitem}\renewcommand{\thehitem}{\alph{hitem}}%
\leavevmode\kern2.5em\llap{({\thehitem})\ }~\parbox[t]{\dimexpr\linewidth-3em}%
{\raggedright#1\vskip0.5\baselineskip\relax}%
\end{minipage}\hskip1em}
\begin{document}
%\hrulefill
\begin{hlist}
\hitem{Frere,}
\hitem {Jacques}
\hitem{Dormez}
\hitem {vous}
\hitem{Sonnez}
\hitem{les}
\hitem{matines!}
\hitem {Din,}
\hitem {dan, dong.}
\end{hlist}
\begin{hlist}[4]
\hitem{Frere Jacques, Frere Jacques}
\hitem {Dormez vous? dormez vous?}
\hitem{Sonnez les matines! Sonnez les matines!}
\hitem {Din, dan, dong. Din, dan, dong.}
\hitem{Frere Jacques, Frere Jacques}
\hitem {Dormez vous? dormez vous?}
\hitem{Sonnez les matines! Sonnez les matines!}
\hitem {Din, dan, dong. Din, dan, dong.}
\hitem{Frere Jacques, Frere Jacques}
\hitem {Dormez vous? dormez vous?}
\end{hlist}
\end{document}