TransWikia.com

How to extract specific and delimited parts in .tex file for Flashcards?

TeX - LaTeX Asked by ahmedfarrag17 on August 1, 2021

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!

One Answer

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
letprotected@iwriteprotected@write
% patch the copy to add immediate
xpatchcmd{protected@iwrite}{write}{immediatewrite}{}{}

NewEnviron{QandA}{subsection{Questions and Answers}BODY%
  protected@iwritetempfile{letsubsectionrelax}%
    {subsection{Questions and Answers}}
  protected@iwritetempfile{letrelax}{BODY}}
makeatother

newwritetempfile

title{Italy} 
author{ahmedfarrag17} 
begin{document} 
immediateopenouttempfile=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}

immediatecloseouttempfile
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

Answered by Steven B. Segletes on August 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP