如何提取 .tex 文件用于 Flashcards 中的特定部分和分隔部分?

如何提取 .tex 文件用于 Flashcards 中的特定部分和分隔部分?

我是 LaTeX 新手。我想创建学习指南/书籍来准备我的数学考试。但我也需要创建抽认卡(在 Anki 等中)。所以我想要能够从我的长文本文档中提取名为“问题和答案”的特定子部分。例如,如果我们有:

\documentclass[10pt]{article} 
\title{Italy} 
\author{ahmedfarrag17} 
\begin{document} 
\section{Introduction}

Italy is a country consisting of a peninsula delimited by the Alps and
several islands surrounding it. Italy is located in the center of the
Mediterranean Sea, in Southern Europe, and is also often considered
part of Western Europe. A unitary parliamentary republic with Rome as
its capital and largest city, the country covers a total area of
301,340 km2.

\subsection{Questions and Answers}

Where is Italy located?\\Italy is located in the center of the
Mediterranean Sea, in Southern Europe.

What is the capital of Italy?\\Rome.

\section{Borders and Population} 

Italy shares land borders with France, Switzerland, Austria and
Slovenia. Its population is about 60 million people.

\subsection{Questions and Answers}
Which countries share a border with Italy?
\\France, Switzerland, Austria and Slovenia.
\end{document}

我想要获得一个像这样的新 Tex 文件:

\subsection{Questions and Answers}
Where is Italy located?\\Italy is located in the center of the Mediterranean Sea, in Southern Europe.

\subsection{Questions and Answers}
Which countries share a border with Italy?
\\France, Switzerland, Austria and Slovenia.

其中仅包含“问答”小节(当然包括下面的文字)。

怎么做?(我是 LaTeX 新手,所以我需要一个非常简单的解释:))谢谢!

答案1

这里我介绍一下QandA完成任务的环境。

此环境将把其内容输出到文档中,作为名为 的子节的一部分Questions and Answers。它还将使用 的修改版本\protected@iwrite将子节标题以及环境内容输出到文件(在本例中称为 )userdata.tex

\documentclass[10pt]{article} 
\usepackage{xpatch, environ}
\makeatletter
% get a copy of `\protected@write
\let\protected@iwrite\protected@write
% patch the copy to add \immediate
\xpatchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}

\NewEnviron{QandA}{\subsection{Questions and Answers}\BODY%
  \protected@iwrite\tempfile{\let\subsection\relax}%
    {\subsection{Questions and Answers}}
  \protected@iwrite\tempfile{\let\\\relax}{\BODY}}
\makeatother

\newwrite\tempfile

\title{Italy} 
\author{ahmedfarrag17} 
\begin{document} 
\immediate\openout\tempfile=userdata.tex
\section{Introduction}

Italy is a country consisting of a peninsula delimited by the Alps and
several islands surrounding it. Italy is located in the center of the
Mediterranean Sea, in Southern Europe, and is also often considered
part of Western Europe. A unitary parliamentary republic with Rome as
its capital and largest city, the country covers a total area of
301,340 km2.

\begin{QandA}%
Where is Italy located?\\
Italy is located in the center of the
Mediterranean Sea, in Southern Europe.

What is the capital of Italy?\\
Rome.
\end{QandA}

\section{Borders and Population} 

Italy shares land borders with France, Switzerland, Austria and
Slovenia. Its population is about 60 million people.

\begin{QandA}
Which countries share a border with Italy?\\
France, Switzerland, Austria and Slovenia.
\end{QandA}

\immediate\closeout\tempfile
\end{document}

在此处输入图片描述

内容为userdata.tex

\subsection {Questions and Answers}
Where is Italy located?\\ Italy is located in the center of the Mediterranean Sea, in Southern Europe. \par What is the capital of Italy?\\ Rome.
\subsection {Questions and Answers}
Which countries share a border with Italy?\\ France, Switzerland, Austria and Slovenia.

\protected@iwrite我从 egreg 的回答中得到的修补版本将 \\ 写入文件

相关内容