提取 LaTex 文件的結構,包括註釋

提取 LaTex 文件的結構,包括註釋

我喜歡在文檔中每個段落的開頭寫一個帶有標題的“特殊”註釋,如下所示:

\section{Introduction}

% 1.0 Visually tracking balls is an interesting problem.  
Text related to why tracking balls is important, blah, blah, blah.   
Blah, blah, blah. 

% 1.1 Tracking balls is difficult because of these challenges.  
Text relating the challenges, blah, blah, blah.  
More blah, blah, blah. 

% 2.0 Previous work.  
Text relating the existing work on tracking balls, blah, blah, blah.  
Blah, blah, blah. 

這有助於我保持文件內容結構良好。

我想做的是提取文件的結構:分段命令(即,\chapter\section)和每個段落開頭的「特殊」註釋。
我想解析一個 LaTex 原始檔(或一組文件,如果主文件包含其他來源文件)並產生一個新文件,其中僅包含分段命令和「特殊」註釋,轉換為常規文字(未註釋),或者更好的是,變成項目符號清單。

因此,在前面的程式碼上運行解析器產生的輸出將是:

\section{Introduction}

\begin{itemize}  
  \item 1.0 Visually tracking balls is an interesting problem.  
  \item 1.1 Tracking balls is difficult because of these challenges.  
  \item 2.0 Previous work.  
\end{itemize}

到目前為止,我得到的最佳解決方案是用字元序列標記特殊註釋(即以“%$”開頭),然後在來源檔案中尋找“%$”和分段命令的出現。

提前致謝。

答案1

這可以使用任何命令列工具輕鬆完成,這裡我使用 grep 和 sed (在 Windows 上的 cygwin bash 中),但其他系統也有類似的,或者您可以使用 perl

如果zz.tex是你的原始文件,

命令行為

$ grep "\(sub\)*section\|^%" zz.tex | sed -e '/^\\s/ a \\\\begin{itemize}' -e 's/^%/\\item /' -e '$ a \\\\end{itemize}'

輸出

\section{Introduction}
\begin{itemize}
\item  1.0 Visually tracking balls is an interesting problem.  
\item  1.1 Tracking balls is difficult because of these challenges.  
\item  2.0 Previous work.  
\end{itemize}

答案2

這裡我使用“目錄”方法,我的意思是我將資訊明確寫入 aux 文件,並從那裡處理它。編輯提出兩種方法:1)「XYZecutive 摘要」包含在原始文件中,2)摘要是外部文檔。

在這兩種情況下,我都有 tex 文件,在其中我可以重複使用巨集\addxyzline{type}{content}向 aux 文件添加各種行。我還有\writexyz宏,它將讀取 .aux 檔案並建立 .xyz 檔案。它可以在 後出現在來源文件中\begin{document},可以在發生任何分段之前,也可以在發生所有分段之後。

方法 1:同一文件中的 XYZecutive 摘要

在這種方法中,巨集不僅將\writexyz內容寫入 .xyz 文件,還在第二遍中處理該文檔,適當地格式化內容。

執行摘要(包含所有\addxyzline資訊)出現在呼叫 時\writexyz。在此 MWE 中,我將其放在目錄之後、文件內容之前。我使用\addtocontents巨集定義,以便執行摘要部分不會最終成為目錄中的重複部分。

\documentclass{article}
\newcommand\addxyzline[2]{\addtocontents {xyz}{\protect \xyzline {#1}{#2}}}
\let\svaddtocontents\addtocontents
\makeatletter
\newcommand\writexyz{%
  \renewcommand\addtocontents[2]{\relax}%
  \clearpage\setcounter{section}{0}\noindent%
  {\Huge\hrulefill XYZecutive Summary\hrulefill\par}%
  \newcommand\xyzline[2]{\expandafter\csname##1\endcsname{##2}}\@starttoc{xyz}%
  \par\noindent\hrulefill\clearpage\setcounter{section}{0}%
  \let\addtocontents\svaddtocontents
}
\makeatother
\begin{document}
\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\writexyz

\section{Introduction}
\addxyzline{section}{Introduction}

\addxyzline{begin}{itemize}
\addxyzline{item}{1.0 Visually tracking balls is an interesting problem.}
Text related to why tracking balls is important, blah, blah, blah.   
Blah, blah, blah. 

\addxyzline{item}{1.1 Tracking balls is difficult because of these challenges.}
Text relating the challenges, blah, blah, blah.  
More blah, blah, blah. 

\addxyzline{item}{2.0 Previous work.}
Text relating the existing work on tracking balls, blah, blah, blah.  
Blah, blah, blah.
\addxyzline{end}{itemize}

\end{document}

在此輸入影像描述

方法 2:單獨的摘要文件

在這裡,我像以前一樣創建文檔正文,但\writexyz不會將摘要寫入同一文檔,而僅寫入 .xyz 文件。

\documentclass{article}
\newcommand\addxyzline[2]{\addtocontents {xyz}{\protect \xyzline {#1}{#2}}}
\makeatletter
\newcommand\writexyz{\newcommand\xyzline[2]{}\@starttoc{xyz}}
\makeatother
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\section{Introduction}
\addxyzline{section}{Introduction}

\addxyzline{begin}{itemize}
\addxyzline{item}{1.0 Visually tracking balls is an interesting problem.}
Text related to why tracking balls is important, blah, blah, blah.   
Blah, blah, blah. 

\addxyzline{item}{1.1 Tracking balls is difficult because of these challenges.}
Text relating the challenges, blah, blah, blah.  
More blah, blah, blah. 

\addxyzline{item}{2.0 Previous work.}
Text relating the existing work on tracking balls, blah, blah, blah.  
Blah, blah, blah.
\addxyzline{end}{itemize}

\writexyz
\end{document}

就螢幕輸出而言,編譯會產生原始文件的輸出,而不考慮內容\addxyzline

在此輸入影像描述

但是,它也會建立一個 .xyz 檔案(在本例中為 xyz.xyz,因為我將原始檔案稱為 xyz.tex),其中包含以下資訊:

\xyzline {section}{Introduction}
\xyzline {begin}{itemize}
\xyzline {item}{1.0 Visually tracking balls is an interesting problem.}
\xyzline {item}{1.1 Tracking balls is difficult because of these challenges.}
\xyzline {item}{2.0 Previous work.}
\xyzline {end}{itemize}

包含我的所有\addxyzline資訊。

然後,我有一個非常簡單的第二個 tex 檔:

\documentclass{article}
\newcommand\xyzline[2]{\csname#1\endcsname {#2}\par}
\begin{document}
\input{xyz.xyz}% NAME OF .xyz FILE GOES HERE IN THE ARGUMENT
\end{document}

它提供了 xyz 摘要信息

在此輸入影像描述


後記

正如您所看到的, 的第一個參數\addxyzline是任何巨集名稱的文字。在我的 MWE 中,我使用了「部分」、「開始」、「專案」和「結束」。其他有用的咒語可能包括

\addxyzline{relax}{this is just text added on the same line}
\addxyzline{newline}{this is just text on a new line}

答案3

這是使用“目錄”方法的 ConTeXt 解決方案。選擇一個您未使用的章節標題,說出subsubject並使用它來註釋您的內容大綱在主文件中:

\setuphead[subsubject][placehead=empty, before=,after=, incrementnumber=yes]

\starttext

\section{Introduction}

\subsubject{1.0 Visually tracking balls is an interesting problem.}
Text related to why tracking balls is important, blah, blah, blah.   
Blah, blah, blah. 

\subsubject{1.1 Tracking balls is difficult because of these challenges.}
Text relating the challenges, blah, blah, blah.  
More blah, blah, blah. 

\subsubject{2.0 Previous work. }
Text relating the existing work on tracking balls, blah, blah, blah.  
Blah, blah, blah. 

\stoptext

在第一行中,placehead=empty告訴 ConTeXt 不要將標題(在本例中為子主題)放在輸出中;incrementnumber=yes需要確保目錄條目的編寫方式可以透過其他文件存取。

接下來建立一個單獨的檔案來建立大綱。我假設主文件的名稱是test.tex.

\starttext
\ctxlua{job.load("test.tuc")}
\placelist[section,subsubject]
          [pagenumber=no, headnumber=no]   
\stoptext

這使

在此輸入影像描述

相關內容