
По сути, мне нужно написать один список, например, itemize, пометить каждый элемент одним или несколькими тегами, а затем сгенерировать вывод, который создаст несколько списков на основе тегов, т. е. каждый тег должен быть разделом или чем-то еще:
Я уже нашелэтот:
\documentclass{article}
\usepackage{pgffor}
\usepackage{xstring}
\usepackage{environ}
\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\begin{pro}{Geometry}
Find the area of ...
\end{pro}
\begin{pro}{Trigonometry}
The angle ...
\end{pro}
\begin{pro}{Algebra}
Prove that $x^2+1=0$ has no real solution.
\end{pro}
\begin{pro}{Geometry}
Find the radius ...
\end{pro}
\end{filecontents*}
\NewEnviron{pro}[1]{%
\IfStrEq{#1}{\CurrentSubject}{\item \BODY}{}
}%
\newcommand*{\CurrentSubject}{}%
\begin{document}
\foreach \Title in {Algebra, Geometry, Trigonometry} {%
\renewcommand*{\CurrentSubject}{\Title}%
\section*{\CurrentSubject}
\begin{enumerate}
\input{foo}
\end{enumerate}
}%
\end{document}
Но это, по-видимому, нецелесообразно, поскольку для каждого элемента можно использовать только один тег.
Есть ли что-то, что делает это? Я уже думал о реализации собственного пакета, но я не знаком с программированием tex.
Обновлять
Я нашел способ изменить опубликованный код, чтобы добиться сортировки по нескольким тегам:
\documentclass{article}
\usepackage{pgffor}
\usepackage{xstring}
\usepackage{environ}
\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\begin{pro}{Geometry}{}{}{}{}
Find the area of ...
\end{pro}
\begin{pro}{Trigonometry}{}{}{}{}
The angle ...
\end{pro}
\begin{pro}{Algebra}{Trigonometry}{}{}{}
Prove that $x^2+1=0$ has no real solution.
\end{pro}
\begin{pro}{Geometry}{Algebra}{}{}{}
Find the radius ...
\end{pro}
\end{filecontents*}
\NewEnviron{pro}[5]{%
\IfStrEq{#1}{\CurrentSubject}{\item \BODY}{}
\IfStrEq{#2}{\CurrentSubject}{\item \BODY}{}
\IfStrEq{#3}{\CurrentSubject}{\item \BODY}{}
\IfStrEq{#4}{\CurrentSubject}{\item \BODY}{}
\IfStrEq{#5}{\CurrentSubject}{\item \BODY}{}
}%
\newcommand*{\CurrentSubject}{}%
\begin{document}
\foreach \Title in {Algebra, Geometry, Trigonometry} {%
\renewcommand*{\CurrentSubject}{\Title}%
\section*{\CurrentSubject}
\begin{enumerate}
\input{foo}
\end{enumerate}
}%
\end{document}
Итак, указание количества тегов, которые вы хотите использовать заранее. Это работает, но неудобно - было бы здорово, если бы кто-то знал, как сделать это динамичным.
Если кто-то хочет использовать или внести свой вклад:https://github.com/maalaria/kite
решение1
Следующий пример предоставляет pro
среду для принятия одного обязательного аргумента, который может содержать список тегов, разделенных запятыми. Для каждого тега среда \BODY
сохраняется в макросе <tag>@<num>
, который затем можно распечатать последовательно через \printlist{<tag>}
.
\documentclass{article}
\usepackage{pgffor,environ}
\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\begin{pro}{Geometry}
Find the area of \ldots
\end{pro}
\begin{pro}{Trigonometry}
The angle \ldots
\end{pro}
\begin{pro}{Algebra,Trigonometry}
Prove that $x^2 + 1 = 0$ has no real solution.
\end{pro}
\begin{pro}{Geometry,Algebra}
Find the radius \ldots
\end{pro}
\end{filecontents*}
\makeatletter
\NewEnviron{pro}[1]{%
\foreach \Title in {#1} {%
\expandafter\ifcsname c@\Title\endcsname\else% If a counter doesn't exist...
\newcounter{\Title}% ... create it
\fi
\stepcounter{\Title}% Another element should be added to particular list
\edef\x{% Add element to particular list
\noexpand\expandafter\noexpand\protected@xdef
\noexpand\csname \Title @\csname the\Title\endcsname\noexpand\endcsname{\BODY}}\x
}
}
\makeatother
\newcommand{\processfile}[1]{\input{#1}}
\newcommand{\printlist}[2][itemize]{%
\expandafter\let\expandafter\listend\csname the#2\endcsname
\begin{#1}
\foreach \curitem in {1,...,\listend} {
\item \expandafter\csname #2@\curitem\endcsname
}
\end{#1}
}
\begin{document}
\processfile{foo}% Process file with pro environments
Algebra:
\printlist{Algebra}
Geometry:
\printlist{Geometry}
Trigonometry:
\printlist{Trigonometry}
\end{document}
Обработка файла производится с использованием \processfile{<file>}
.
Проверка на наличие ошибок <tag>
при вызове не выполняется \processlist{<tag>}
, но может быть добавлен.