根據標籤對項目進行排序

根據標籤對項目進行排序

我基本上想要的是編寫一個列表,例如 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>},但可以添加。

相關內容