假设我有一个包含 n 个项目的列表,是否可以创建一个 2 乘 n+1 的数组?
我有我的 MWE
\documentclass{article}
\usepackage{enumitem}
\begin{document}
%Example 1
\begin{enumerate}[label=\textbf{\arabic*}\hspace{4mm}, ref=\arabic*]
\item This is item 1.
\item This is item 2.
\item This is item 3.
\end{enumerate}
%Example 2
\begin{enumerate}[label=\textbf{\arabic*}\hspace{4mm}, ref=\arabic*]
\item This is item 1.
\item This is item 2.
\item This is item 3.
\item This is item 4.
\item This is item 4.
\item This is item 6.
\end{enumerate}
\end{document}
答案1
定义一个特定的环境,并在其后添加表。
\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01
\usepackage{enumitem}
\usepackage{tabularx}
\newlist{enumtable}{enumerate}{1}
\setlist[enumtable]{label=\textbf{\arabic*}\hspace{4mm}, ref=\arabic*}
\AddToHook{env/enumtable/after}{\printtable}
\ExplSyntaxOn
\NewDocumentCommand{\printtable}{}
{
\noindent
\begin{tabularx}{\textwidth}{@{}|*{\int_eval:n { \value{enumtablei}+1 }}{X|}@{}}
\hline
Number \int_step_function:nN { \value{enumtablei} } \alanjones_addnumber:n \\
\hline
Item \prg_replicate:nn { \value{enumtablei} } { & } \\
\hline
\end{tabularx}
}
\cs_new:Nn \alanjones_addnumber:n { & #1 }
\ExplSyntaxOff
\begin{document}
\begin{enumtable}
\item This is item 1.
\item This is item 2.
\item This is item 3.
\end{enumtable}
%Example 2
\begin{enumtable}
\item This is item 1.
\item This is item 2.
\item This is item 3.
\item This is item 4.
\item This is item 4.
\item This is item 6.
\end{enumtable}
\end{document}
如果你的 LaTeX 无法识别\AddToHook
,不要担心:还要加载\usepackage{etoolbox}
并替换行
\AddToHook{env/enumtable/after}{\printtable}
和
\AfterEndEnvironment{enumtable}{\printtable}
但是,如果您更新 TeX 系统会更好。
为了打印单身的最后的表格:
\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01
\usepackage{enumitem}
\usepackage{tabularx}
\newlist{enumtable}{enumerate}{1}
\setlist[enumtable]{label=\textbf{\arabic*}\hspace{4mm}, ref=\arabic*}
\AddToHook{env/enumtable/after}{\maketable\AtEndDocument{\printtable}}
\ExplSyntaxOn
\box_new:N \g__alanjones_finaltable_box
\NewDocumentCommand{\maketable}{}
{
\vbox_gset:Nn \g__alanjones_finaltable_box
{
\noindent
\begin{tabularx}{\textwidth}{@{}|*{\int_eval:n { \value{enumtablei}+1 }}{X|}@{}}
\hline
Number \int_step_function:nN { \value{enumtablei} } \alanjones_addnumber:n \\
\hline
Item \prg_replicate:nn { \value{enumtablei} } { & } \\
\hline
\end{tabularx}
}
}
\cs_new:Nn \alanjones_addnumber:n { & #1 }
\NewDocumentCommand{\printtable}{}
{
\par
\addvspace{\topsep}
\box_use:N \g__alanjones_finaltable_box
}
\ExplSyntaxOff
\begin{document}
\begin{enumtable}
\item This is item 1.
\item This is item 2.
\item This is item 3.
\item This is item 4.
\item This is item 4.
\item This is item 6.
\end{enumtable}
Some text here.
\end{document}
对旧版本 LaTeX 的修改与上面所述的类似。
答案2
和针织品:
\documentclass{article}
\begin{document}
<<echo=F>>=
items = c("foo","bah","baz")
@
\begin{itemize}
\item
<<results="asis", echo=F>>=
cat(items, sep="\n\\item ")
@
\end{itemize}
<<results="asis", echo=F>>=
library(xtable)
print(xtable(t(items)),include.rownames=FALSE)
@
\end{document}