2

I am new to LaTeX. I want to create study guides/books for preparation of my maths exams. But I need to create Flashcards as well (in Anki etc.). So what I want is to be able to extract from my long tex document specific subsections called "Questions and Answers". For example, if we have:

\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}

I want to obtain a new Tex file like this:

\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.

that has only that Questions and Answers subsection (including the text underneath of course).

How to do it? (I am new to LaTeX so I need a very simple explanation please :) ) Thank you!

  • Welcome to the site. Can you edit your question and provide a small but complete (beginning with \documentclass) example showing as best you can what you have to work with and what you are trying to extract? – Steven B. Segletes Jul 27 '21 at 18:37
  • 1
    I did it now! thank you! – ahmedfarrag17 Jul 28 '21 at 10:02
  • @ahmedfarrag17, kindly have a look at https://tex.stackexchange.com/questions/605146/how-to-extract-equation-environments-or-other-blocks-inside-a-latex-document , which mentions several ways to extract parts. Most require some kind of markup, e.g. a specific environment to find it. The script solution can extract anything ... if you know how to tame the regular expressions for this purpose ;-) – MS-SPO Jul 31 '21 at 10:23

1 Answers1

2

Here, I introduce the QandA environment to accomplish the task.

This environment will output its contents into the document as part of a subsection named Questions and Answers. It will also use a modified version of \protected@iwrite to output the subsection header as well as the environment contents to a file, in this case called 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}

enter image description here

the contents of userdata.tex are:

\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.

The patched version of \protected@iwrite I got from egreg's answer at Writing \\ to a File