タグに従ってアイテムを並べ替える

タグに従ってアイテムを並べ替える

私が基本的に望んでいるのは、単一のリスト (たとえば itemize) を作成し、各項目に 1 つ以上のタグを付けて、タグに基づいて複数のリストを作成する出力を生成することです。つまり、各タグはセクションなどである必要があります。

ここに画像の説明を入力してください

すでに見つけたこれ:

\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}

しかし、これは適切ではないようです。アイテムごとに 1 つのタグしか使用できません。

これを実行するものはありますか。独自のパッケージを実装することをすでに考えていましたが、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>}が、 が追加される可能性があります。

関連情報