LaTeX가 목록을 자동으로 항목화할 수 있습니까?

Nov 17 2020

그래서 .txt형식 의 단어 목록 파일이 있습니다 .

나는 그것을 편집하고 열거하고 싶습니다. 그러나 파일이 잠재적으로 수천 줄로 실행되기 때문에 수동으로 수행하는 것은 어려운 작업입니다. 모든 단어를 자동으로 열거하고 (그리고 각 단어에 대해 다른 작은 반복적 인 변경을 수행) 더 나은 방법으로 무작위화할 수 있는지 궁금합니다.

NB LaTeX에서 수행 할 수있는 작업인지 또는 수행해야하는 작업인지 정확히 알 수 없으므로 다른 제안도 환영합니다. 미리 감사드립니다.

편집 : 작은 반복적 인 변경을 의미 할 때 각 단어 뒤에 ____를 그리는 것과 같은 것을 염두에 둡니다.

따라서 본질적으로 세 가지 작업을 수행하려면 LaTex가 필요합니다.

  1. 파일을 읽고 항목 화
  2. 단어마다 줄을 긋다
  3. 전체 목록을 무작위로 추출하십시오.

죄송합니다. 질문 형식을 더 잘 지정 했어야합니다.

답변

31 DavidCarlisle Nov 17 2020 at 15:22

\read텍스트 파일을 한 줄씩 읽는 데 사용할 수 있습니다.

wordlist.txt

red
orange
yellow
green
blue
indigo
violet

file.tex

\documentclass{article}

\newread\wordlist
\openin\wordlist=wordlist.txt
\begin{document}
\def\blankline{\par}

\begin{enumerate}
\loop\ifeof\wordlist
\else
\read\wordlist to \thisword
\ifx\thisword\blankline
\else
\item \thisword
\fi
\repeat
\end{enumerate}
\end{document}
18 Sam Nov 17 2020 at 15:40

csvsimple패키지를 사용하여 솔루션을 제안 합니다.

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents}{list.csv}
religion
religious
rely
remain
\end{filecontents}

\begin{document}

\csvloop{
    file = {list.csv},
    no head,
    before reading = {\begin{enumerate}},
    after reading = {\end{enumerate}},
    before line = \item
}

\end{document}

편집하다

요소 뒤에 선을 그리려면 다음을 사용하십시오 \rule.

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents}{list.csv}
religion
religious
rely
remain
\end{filecontents}

\begin{document}

\csvloop{
    file = {list.csv},
    no head,
    before reading = {\begin{enumerate}},
    after reading = {\end{enumerate}},
    before line = \item,
    after line = \rule{1cm}{.4pt}
}

\end{document}

LaTeX는 열거 형의 개별 항목을 저장하지 않기 때문에 항목을 섞는 것이 까다 롭습니다. 이 문제에 대한 몇 가지 무거운 해결책 이 있지만 대규모 데이터 세트 ( "잠재적으로 수천 줄")를 처리 할 때 권장하지 않습니다. 문서를 컴파일하기 전에 목록을 섞는 Python 스크립트를 빠르게 설정하십시오.

import random

with open('data.csv', 'r') as input_file:
    data = input_file.readlines()

random.shuffle(data)

with open('output_file.csv', 'w') as output_file:
    for item in data:
        output_file.writelines(item)

pythontexLaTeX 파일에 실행 가능한 Python 코드를 추가 할 수 있는 패키지를 볼 수도 있습니다 . 문서 생성시 코드가 실행되고 결과가 LaTeX 파일에 추가됩니다. 나는 이것이 문서 생성에 셔플 링을 구현할 수 있다고 상상할 수 있습니다.

10 benjamin Nov 17 2020 at 20:54

나는 이것을 잠시 배우고 싶었 기 때문에 LuaLaTeX를 사용한 Sam의 답변 버전입니다. 내가 좋아하는 점은 LaTeX에 완전히 내장되어 있으며lualatex

\documentclass{article}
\usepackage{luacode}

\begin{filecontents*}{list.csv}
religion
religious
rely
remain
\end{filecontents*}

\begin{document}

\begin{itemize}

  \begin{luacode}
  io.input("list.csv")
  -- use a table to store the lines of the file
  local lines = {}
  -- read the lines in table 'lines'
  for line in io.lines() do
    table.insert(lines, line)
  end
  -- use another table to store a shuffled version of lines
  shuffled = {}
  -- for each line, pickup a position in the shuffled table 
  -- and insert the line there
  for i, line in ipairs(lines) do
    local pos = math.random(1, #shuffled+1)
    table.insert(shuffled, pos, line)
  end

  -- write all the lines, after an item and with a rule after it
  for i, line in ipairs(shuffled) do 
    tex.sprint("\\item ", line, " \\rule{1cm}{.4pt}") end
  \end{luacode}

\end{itemize}

\end{document}
10 AlexG Nov 17 2020 at 23:05

LaTeX3는 우아함과 간결함면에서 탁월합니다. 😉 :

\documentclass{article}
\usepackage{expl3}

\begin{filecontents*}{list.csv}
religion
religious
rely
remain
\end{filecontents*}

\begin{document}

\begin{itemize}

\ExplSyntaxOn
  \seq_new:N\l_input_seq
  \ior_new:N\l_file_stream
  \ior_open:Nn\l_file_stream{list.csv}
  \ior_str_map_inline:Nn\l_file_stream{ \seq_put_right:Nn\l_input_seq{#1} }
  \ior_close:N\l_file_stream
  \seq_shuffle:N\l_input_seq
  \seq_map_inline:Nn\l_input_seq{\item~#1}
\ExplSyntaxOff

\end{itemize}

\end{document}
4 benjamin Nov 19 2020 at 14:30

Sam이 조사하라고 pythontex했기 때문에 결과는 LaTeX 파일에 Python 코드를 포함하는 솔루션입니다. LaTeX를 스크립팅 / 프로그래밍과 인터페이스하는 문제에 대해 서로 다른 솔루션을 갖는 것이 좋은 일이라고 생각합니다.

3 단계로 컴파일 :

  1. *LaTeX myfile
  2. pythontex myfile
  3. *LaTeX myfile

myfile.tex :

\documentclass{article}
\usepackage{pythontex}

\begin{filecontents*}{list.csv}
religion
religious
rely
remain
\end{filecontents*}

\begin{document}

\begin{pycode}
import random

with open('list.csv', 'r') as input_file:
    data = input_file.readlines()

random.shuffle(data)

print(r'\begin{enumerate}')
for item in data:
     print(r'\item ', item, r'\rule{1cm}{.4pt}')
print(r'\end{enumerate}')
\end{pycode}

\end{document}