태그에 따라 항목 정렬

태그에 따라 항목 정렬

내가 기본적으로 원하는 것은 단일 목록을 작성하는 것입니다. 예를 들어 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>}추가될 수 있습니다.

관련 정보