我一直在寻找一种方法来动态创建一个由n
条目组成的逗号分隔列表,所有条目相等,由\newcommand\mytext{species:}
(示例)定义,并且n
是 中的条目数\newcommand\mylist
。\newcommand\listtobegenerated
显示了定义的列表应如何显示。有什么办法吗?
我找到了这个:从逗号分隔列表中删除元素,我想我可以通过将删除改为替换来修改它,但接受的答案\count@
和\toks@
给了我错误,我无法解决,并认为一定有一些更干净的方法而不是使用\count@
和。该问题的其他答案是针对 ConText 和 expl3 的,我正在寻找 LaTeX 答案。那么,我如何根据的内容和中的元素数量\toks@
生成的内容?listtobegenerated
mytext
mylist
谢谢 :)
\documentclass[12pt]{article}
\usepackage[american]{babel}
\usepackage{graphicx}
\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}
\newcommand\mytext{species:}
% this is the the list I attempt to generated based on the number of elements in mylist and the content of mytext:
\newcommand\listtobegenerated{species:,species:,species:,species:,species:}
% preamble:
\def\preamble{\expandafter\xpreamble\mylist,\relax,}
\def\xpreamble#1,{%
\ifx\relax#1%
\else
c%
\expandafter\xpreamble
\fi}
% tablestart:
\def\tablestart{%
\edef\temp{\noexpand\begin{tabular}{c\preamble}\noexpand\hline}%
\temp}
% listheadings:
\def\listheadings{%
\expandafter\xlistheadings\listtobegenerated,\relax,%
\expandafter\xlistheadings\mylist,\relax,%
\hline}
% xlistheadings
\def\xlistheadings#1,{%
\ifx\relax#1%
\expandafter\\
\else
&\textbf{#1}%
\expandafter\xlistheadings
\fi}
\begin{document}
\begin{table}
\scalebox{0.7}{
\tablestart
\listheadings
\end{tabular}
} % end scalebox
\caption{Caption goes here.}
\begin{enumerate}
\item Item number one.
\item Item number two.
\end{enumerate}
\end{table}
\end{document}
答案1
看上去好像要搭建一个表格环境,这样经过很多无用的步骤效率不太高:
\documentclass{article}
\usepackage{xparse,graphicx}
\ExplSyntaxOn
\NewDocumentCommand{\generatetable}{mm}{ \nrz_generate_table:nn { #1 } { #2 } }
\cs_new:Npn \nrz_generate_table:nn #1 #2
{
\tl_clear:N \l_nrz_preamble_tl
\tl_set:Nn \l_nrz_preamble_tl { c@{} }
\tl_clear:N \l_nrz_header_one_tl
\tl_clear:N \l_nrz_header_two_tl
\clist_map_inline:Nn #1
{
\tl_put_right:Nn \l_nrz_preamble_tl { c }
\tl_put_right:Nn \l_nrz_header_one_tl { & }
\tl_put_right:NV \l_nrz_header_one_tl #2
\tl_put_right:Nn \l_nrz_header_two_tl { & }
\tl_put_right:Nn \l_nrz_header_two_tl { ##1 }
}
\tl_put_right:Nn \l_nrz_header_one_tl { \\ }
\tl_put_right:Nn \l_nrz_header_two_tl { \\ }
\nrz_build_table:
}
\cs_new:Npn \nrz_build_table:
{
\resizebox{\textwidth}{!}{
\begin{tabular}{\l_nrz_preamble_tl}
\tl_use:N \l_nrz_header_one_tl
\tl_use:N \l_nrz_header_two_tl
\end{tabular}
}
}
\tl_new:N \l_nrz_preamble_tl
\tl_new:N \l_nrz_header_one_tl
\tl_new:N \l_nrz_header_two_tl
\ExplSyntaxOff
\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}
\newcommand\mytext{species:}
\begin{document}
\noindent\generatetable{\mylist}{\mytext}
\end{document}
用设施做同样的事情并不困难etoolbox
。但我认为这更容易理解。
如果你真的想要建立逗号分隔列表,你可以这样做
\makeatletter
\newcommand\generatefromwith[3]{%
\gdef#1{\@gobble}%
\@for\next:=#2\do
{\expandafter\g@addto@macro\expandafter#1\expandafter{\expandafter,#3}}%
\expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter#1\expandafter\expandafter\expandafter{#1}
}
\makeatother
然后
\newcommand\mylist{Haliaeetus albicilla,Lagidium viscacia,Balaenoptera musculus,Panthera pardus orientalis,Human}
\newcommand\mytext{species:}
\generatefromwith\myheader\mylist\mytext
将定义\myheader
扩展为
species:,species:,species:,species:,species: