다양한 라텍스 명령의 내용을 추출하고 싶습니다. 예:
- 부분
- 장
- 표제
- 사이드노트 등
파일 에서 .tex
. 이러한 요소는 결국 CSV 파일로 생성됩니다.
chapter, chapter 1 title text
chapter, chapter 2 title text
figure, figure text
여기에는 정규 표현식이 필요합니까, 아니면 바퀴를 재발명하는 것입니까?
파일 .tex
은 pandoc을 통해 마크다운 문서에서 생성됩니다.
예시 A
\documentclass{report}
\usepackage{hyperref}
\newcommand*\Quux{Pouet}
\begin{document}
\chapter[foo]{Foo}
\url{http://www.tex.org}
\end{document}
장과 URL을 추출하고 싶습니다.
예시 B
\documentclass{tufte-book}
\begin{document}
\chapter{A chap}
\marginnote{A note}
\end{document}
여백을 추출하고 싶습니다.
답변1
다음과 같은 파일을 사용하여 모든 섹션화 명령을 계측할 수 있습니다(라고 부르겠습니다 instrumenter.tex
).
\RequirePackage{xparse}
\ExplSyntaxOn
\iow_new:N \l_csgillespie_iow
% #1: sectioning command name as a single token
% #2: character tokens representing the command name, without the backslash
\cs_new_protected:Npn \csgillespie_instrument_sec_cmd:Nn #1#2
{
% Save the original sectioning command
\cs_gset_eq:cN { g_csgillespie_#2_orig: } #1
\RenewDocumentCommand #1 { s O{##3} m }
{
\iow_now:Nn \l_csgillespie_iow { #2 ; ##2 ; ##3 }
\IfBooleanTF {##1}
{ \use:c { g_csgillespie_#2_orig: } * {##3} }
{ \use:c { g_csgillespie_#2_orig: } [##2] {##3} }
}
}
\cs_generate_variant:Nn \csgillespie_instrument_sec_cmd:Nn { c }
% #1: character tokens representing the command name, without the backslash
\cs_new_protected:Npn \csgillespie_instrument_sec_cmd_ifexists:n #1
{
\cs_if_exist:cT {#1}
{ \csgillespie_instrument_sec_cmd:cn {#1} {#1} }
}
\AtBeginDocument
{
\iow_open:Nn \l_csgillespie_iow { sectioning.csv }
\clist_map_inline:nn
{
part, chapter, section, subsection, subsubsection, paragraph,
subparagraph
}
{ \csgillespie_instrument_sec_cmd_ifexists:n {#1} }
}
\AtEndDocument { \iow_close:N \l_csgillespie_iow }
\ExplSyntaxOff
\endinput
그런 다음 다음과 같은 파일을 확인한다고 가정합니다 tested.tex
.
\documentclass{report}
\newcommand*\Quux{Pouet}
\begin{document}
\chapter[foo]{Foo}
\chapter{Bar \emph{Baz!}}
\chapter*{\Quux !}
\section{A \emph{section}!}
\section[Short title]{Another section}
\end{document}
당신이 해야 할 일은 instrumenter.tex
에 있는 위치 TEXINPUTS
(아마도 와 동일한 디렉토리 tested.tex
)를 입력한 후 다음을 실행하는 것입니다.
latex '\input instrumenter \input tested'
(여기서 작은 따옴표는 쉘에 대한 것이므로 쉘에 맞게 조정하십시오). 이 예에서는 다음과 같은 파일이 sectioning.csv
동일한 디렉터리에 생성됩니다 .tested.tex
chapter;foo;Foo
chapter;Bar \emph {Baz!};Bar \emph {Baz!}
chapter;\Quux !;\Quux !
section;A \emph {section}!;A \emph {section}!
section;Short title;Another section
일부 제목에 세미콜론 구분 기호( ;
)가 포함된 경우 출력(또는 CSV 형식에 필요한 모든 것)에 큰따옴표를 추가할 수 있습니다. 다음 줄에 추가하면 됩니다.
\iow_now:Nn \l_csgillespie_iow { #2 ; ##2 ; ##3 }
논쟁의 단락
\par
이 코드를 사용하여 다른 명령을 래핑하고 이러한 명령이 합법적으로 인수에 토큰(예: 빈 줄)을 가질 수 있는 경우 +
다음과 같이 해당 인수 앞에 a를 추가합니다.
\RenewDocumentCommand #1 { s +O{##3} +m }
는 O{##3}
래핑된 명령의 선택적 인수(기본값은 필수 인수 값), m
필수 인수에 해당합니다. 표준 섹션화 명령에는 유용 +
하지 않지만 이 시스템을 사용하여 다른 명령의 인수를 기록하는 경우에는 유용할 수 있습니다.
답변2
추출 패키지를 사용하면 다음과 같은 작업을 수행할 수 있습니다.
https://www.ctan.org/tex-archive/macros/latex/contrib/extract
거기에 있는 추가 정보(extract.pdf)의 문서는 꽤 좋습니다.