提取长文档的骨架

提取长文档的骨架

我正在写一篇长篇文档,其中的关键信息位于环境中(逐项列出要做的事情,方程式、引理、定理等),并由文本和证明分隔。

为了方便与合著者的沟通,我想制作一个较短版本的文档,其中仅包含结构(章节、小节……)和一些环境(实际上除了证明之外的所有内容)。

例如:

\documentclass{amsart}
\newtheorem{theorem}{Theorem}

\begin{document}
\section{Intro}
\begin{itemize}
\item todo 1
\item todo 2
\end{itemize}
bla bla bla
\section{First result}
\begin{theorem}
(hopefully) nice thing
\end{theorem}
\begin{proof}
bla bla bla
\begin{equation}
E=mc^2
\end{equation}
\end{proof}
\end{document}

应该编译为

\documentclass{amsart}
\newtheorem{theorem}{Theorem}

\begin{document}
\section{Intro}
\begin{itemize}
\item todo 1
\item todo 2
\end{itemize}
\section{First result}
\begin{theorem}
(hopefully) nice thing
\end{theorem}
\end{document}

这可以手动完成,但我的目标是更高效的解决方案。你能找到一个吗?

答案1

我编写了以下 python 代码,它似乎运行良好。

"""
Created on Tue Feb 25 12:38:06 2020

@author: benoit
inspired by CommentIO.py of Martin Larsson, [email protected]
"""

import sys
import os
import os.path

def printHelpMessage(error):
    print("\nError: " + error )
    sys.exit()


# Check if the input file exists.
inputFilename="Draft.tex";
outputFilename = "miniSkeleton.tex";
envinclude=["theorem", "lemma","itemize","subequations","equation","prop","align", "remark"]
envexclude=["proof"] 
includesections= True



if not (os.path.isfile(inputFilename) and os.access(inputFilename, os.R_OK)):
    printHelpMessage("Input file is either missing or is not readable.")


def issection(l):
    return ("\\section{" in l) or ("\\subsection{" in l) or ("\\subsubsection{" in l)

def modifydepth(envlist,line):
    for i,j in enumerate(envlist):
        if ("\\begin{"+j) in line:
            return 1
        else:
            if("\\end{"+j) in line:
                return -1
    return 0

started=False
ended=False
includedepth=0
excludedepth=0
outputcontent = []
# Read the file into a list.
with open(inputFilename) as f:
    content = f.readlines()

# Go through each line in the list.
for i, checkLine in enumerate(content):
    if "\\begin{document}" in checkLine:
        started=True;
        outputcontent.append(checkLine);
    if started and not(ended):
        excludedepth=excludedepth+modifydepth(envexclude,checkLine)
        if (excludedepth==0):
            mincludedepth=modifydepth(envinclude,checkLine)
            if (includedepth>0) or (mincludedepth==1):
                outputcontent.append(checkLine);
            else:
                if issection(checkLine):
                    outputcontent.append(checkLine);
            includedepth=includedepth+mincludedepth
    else:
        outputcontent.append(checkLine);
    if "\\end{document}" in checkLine:
        ended=True;
        outputcontent.append(checkLine);
# Write the list to a new file.
output = open(outputFilename, "w")
output.writelines(outputcontent)
output.close()

答案2

由于您想隐藏证明以及纯文本和其他部分,我只需将这些部分标记为\showtrue序言中的条件:

\documentclass{amsart}
\newif\ifshow
% \showtrue %%%% uncomment to show all 
\newtheorem{theorem}{Theorem}
\begin{document}
\section{Intro}
\begin{itemize}
\item todo 1
\item todo 2
\end{itemize}
\ifshow
bla bla bla
\fi
\section{First result}
\begin{theorem}
(hopefully) nice thing
\end{theorem}
\ifshow
\begin{proof}
bla bla bla
\begin{equation}
E=mc^2
\end{equation}
\end{proof}
\fi
\end{document}

相关内容