LaTeX的列表(list)通常是将项(item,条目)一个一个垂直的平行显示,所谓“列”表的由来。
水平分列列表,即将多个项分散到各列而不是一列,在出考卷的选择题时常碰到,LaTeX中的包有:enumcols(enumlists)、tasks(exsheets)
enumcols宏包是李清创建的,TeXlive 2018后,与CTeX的模板冲突。本篇主要讨论tasks宏包的使用。tasks宏包的主要用法如下:
egin{tasks}[<选项>](<列数>)
ask item1
ask item2
end{tasks}
例:选择题的文档
documentclass{ctexart}
% 导言区添加
usepackage{tasks}
settasks{ %设置
counter-format=tsk[A], %编号
label-format={fseries}, %加粗
%item-indent={-0.1em},
%label-offset={-0.05em}
}
% 测试用
ewcommand{sample}{This is some sample text we will use to create a somewhat longer text spanning a
few lines. This is some sample text we will use to create a somewhat longer text
spanning a few lines}
egin{document}
选择题有4个选项,分4列显示选项A,B,C,D。
egin{enumerate}
egin{tasks}(4)
ask $x^2 + y^2 = R^2$
ask $mathrm{E}=mathrm{MC}^2$
ask $e^{-ipi} = 1$
ask sample
end{tasks}
end{enumerate}
end{document}
例:跨越多个列的项
egin{tasks}(4)
ask sample
ask* exttt{*}占用后面的所有列sample
ask* 占用后面的所有列sample
ask*(2) exttt{*}本身可以带选项,指定占用的列数sample
ask*[(x)] 指定该条目的编号为(x)
startnewitemline
ask! 另起新条目并占用以后的所有列sample
ask sample
end{tasks}
基于tasks定义新列表环境
NewTasks{itemtasks}[item] %以item为分隔符
% 文档中
egin{itemtasks}(3)
item sample
item sample
item sample
end{itemtasks}
使用如下定义的环境mytasks
不能嵌套在enumerate
环境,提示
“begin{mytasks} on input line 22 ended by end{itemtasks}”
或
“egin{itemtasks} on input line 22 ended by end{document}”
NewTasks{itemtasks}[item] % 以item为分隔符
ewenvironment{mytasks}[1][2]{
egin{itemtasks}(#1)
}{
end{itemtasks}
}
% 文档中
egin{enumerate}
item 第一个问题选项如下
egin{mytasks}[2]
item $x^2 + y^2 = R^2$
item $mathrm{E}=mathrm{MC}^2$
item $e^{-ipi} = 1$
item sample
end{mytasks}
end{enumerate}
mytasks
是按照NewEnviron
(xparse包)定义的,标准的环境定义方法使用itemtasks...enditemtasks
ewenvironment{mytasks}[1][2]{
itemtasks(#1)
}{
enditemtasks
}
% 文档中
egin{enumerate}
item 第一个问题选项如下
egin{mytasks}[4]
item $x^2 + y^2 = R^2$
item $mathrm{E}=mathrm{MC}^2$
item $e^{-ipi} = 1$
item sample
end{mytasks}
end{enumerate}
使用environ'包的
NewEnviron`命令定义
NewTasks{itemtasks}[item] % 以item为分隔符
usepackage{environ}
NewEnviron{mytasksenv}[1][2]{
def empbegin{egin{itemtasks}(#1)}
expandafter empbeginBODY
end{itemtasks}
}{}
% 文档中
egin{mytasksenv}[4]
item $x^2 + y^2 = R^2$
item $mathrm{E}=mathrm{MC}^2$
item $e^{-ipi} = 1$
item $x^2 + y^2 = R^2$
end{mytasksenv}
使用xparse'包的
NewDocumentEnvironment`定义
usepackage{xparse}
NewDocumentEnvironment{mytasksx}{}
{
ewTask(2)}
{endnewTask}
完整的示例
%!TEX program = xelatex
documentclass[UTF8]{ctexart}
usepackage{url}
usepackage{tasks}
settasks{
counter-format=tsk[A],
label-format={fseries},
%item-indent={-0.1em},
%label-offset={-0.05em}
}
NewTasks{itemtasks}[item]
%
ewenvironment{mytasks}[1][2]{
% egin{itemtasks}(#1)
%}{
% end{itemtasks}
%}
ewenvironment{mytasks}[1][2]{
itemtasks(#1)
}{
enditemtasks
}
%https://tex.stackexchange.com/questions/237230/tasks-in-custom-environment
usepackage{environ}
NewEnviron{mytasksenv}[1][2]{
def empbegin{egin{itemtasks}(#1)}
expandafter empbeginBODY
end{itemtasks}
}{}
usepackage{xparse}
%https://tex.stackexchange.com/questions/391459/nesting-tasks-within-an-xparse-environment
NewDocumentEnvironment{mytasksx}{}
{itemtasks(2)}
{enditemtasks}
ewcommand{sample}{This is some sample text we will use to create a somewhat longer text spanning a few lines. This is some sample text we will use to create a somewhat longer text spanning a few lines}
egin{document}
egin{mytasksenv}[4]
item six, 继续上面的编号.
item seven
item seven
item seven
end{mytasksenv}
egin{enumerate}
item 第一个问题选项如下
egin{mytasks}[2]
item $x^2 + y^2 = R^2$
item $mathrm{E}=mathrm{MC}^2$
item $e^{-ipi} = 1$
item sample
end{mytasks}
end{enumerate}
egin{tasks}(4)
ask* 一般使用格式为 exttt{\begin{tasks}[选项](列数)}
ask 可使用 exttt{*}
ask three
end{tasks}
egin{tasks}[resume=true]
ask six, 继续上面的编号.
ask seven
end{tasks}
This is some sample text we will use to create a somewhat longer text spanning a
few lines.
egin{enumerate}
item 在LyX中,定义新环境itemtasks,用item分隔符!不会与外面的item冲突!但是LyX插入的选项放在中括号汇总,而tasks要求放在小括号中。
item url{http://troubleshooters.com/linux/lyx/ownlists.htm}不知道说啥
item 第二个问题有如下的选项
egin{tasks}(4)
ask $x^2 + y^2 = R^2$
ask $mathrm{E}=mathrm{MC}^2$
ask $e^{-ipi} = 1$
ask sample
end{tasks}
item 第三
egin{tasks}(2)
ask $x^2 + y^2 = R^2$
ask $mathrm{E}=mathrm{MC}^2$
ask $e^{-ipi} = 1$
ask $e^{-ipi} = 1$
end{tasks}
end{enumerate}
egin{tasks}(4)
ask sample
ask* exttt{*}占用后面的所有列sample
ask* 占用后面的所有列sample
ask*(2) exttt{*}本身可以带选项,指定占用的列数sample
ask*[(x)] 指定该条目的编号为(x)
startnewitemline
ask! 另起新条目并占用以后的所有列sample
ask sample
end{tasks}
end{document}