# To write this Pseudocode with Regex

I am doing a Makefile which I run regularly by Crontab every day at 0230

crontab -e; 30 2 * * * /bin/thePseudocode


Python-like Pseudocode

directories = ["Cardiology", "Rheumatology", "Surgery"]
for directory in directories
files = directory.files(); % not sure if such a parameter exists
files = files.match(.*tex); % trying to get only tex files; not sure if match exists
summaryFile = "";
for texFile in files
textFile.match( (?s)\\begin{question}.*?\\end{question} ) ...
)
% Solution based on this thread
% Problem in writing this Regex in Perl http://unix.stackexchange.com/questions/159307/to-match-only-innermost-environment-by-regex
end
end

save this summaryFile as /Users/Masi/Dropbox/QuestionSummary.tex


where files is the list of all files in the directory, and summaryFile is the file which lists all questions in all tex-files. Those files, I want finally compile by pdflatex and read each morning by pdf-reader.

Example file which locates in the folder Rheumatology

\section{Takayasu arteritis}

\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}

\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}

Fever of unknown origin can be used when you do not know what is causing the disease.

% Show cases in MedScape and ask class.

Aneurysms.

\subsection{Treatment}

\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}

\begin{question}
{When is the checkup of the Takayasu arteritis?}
Only once per year.
You could expect every month like normally in this kind of diseases.
But only once per year.
\end{question}


where the output should be for all files in the folder

\section{Rheumatology}

\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}

\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}

\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}

\begin{question}
{When is the checkup of the Takayasu arteritis?}
Only once per year.
You could expect every month like normally in this kind of diseases.
But only once per year.
\end{question}


Makefile

all:
pdflatex /Users/Masi/Dropbox/QuestionsSummary.tex /Users/Masi/Dropbox/QuestionsSummary.pdf
pdflatex /Users/Masi/Dropbox/QuestionsSummary.tex /Users/Masi/Dropbox/QuestionsSummary.pdf % to compile a few times to be successful
pdflatex
% I am not sure if I should have some error management, since often the pdflatex crashes
% So pdflatex is not probably the right tool to go


How can you such a pseudocode in any tools preferred by you? I like Python but would not make all by it.

• Please explain your pseudocode in words as well. Presumably, files will be the list of all files in the directory right? What's masterfile.add? Ideally, show us an example of your input files and what you want your script to do to them. – terdon Oct 4 '14 at 20:32
• @terdon Thank you for your comment! I changed the masterFile's name to summaryFile to be more descriptive. I mean by masterfile.add to add the output of the function to the end of the masterfile. – Léo Léopold Hertz 준영 Oct 4 '14 at 20:41
• Thanks for the edit. So what would the output of this script look like? I understand that you want to extract all \begin{question}... \end{question} from all .tex files in a given directory and save those in a new file. Is that correct? If you expect the output to be compilable by LaTeX, presumably you also want it to be within \begin{document} and \end{document} tags right? – terdon Oct 4 '14 at 20:44
• @terdon Yes, you are correct. Yes, of course within begin{document}. I will add those things then last. I edited to body to include the expected output too. – Léo Léopold Hertz 준영 Oct 4 '14 at 20:50

If I understand correctly, you're looking for something like this (in bash):

#!/usr/bin/env bash

## avoid errors if a directory has no *tex files
shopt -s nullglob

directories=("Cardiology" "Rheumatology" "Surgery");

## Change this to set whichever options you want.
printf "%s\n%s\n" "\documentclass{YOURCLASS}" "\begin{document}"

for directory in ${directories[@]} do ## Reset the counter, avoid empty sections. c=0; for file in "$directory"/*tex
do
let c++
[ "$c" -eq 1 ] && printf "\n%s\n" "\section{$directory}"
## Extract the wanted lines
perl -lne '$a=1 && print "" if /\\begin{question}/; print if$a==1;
$a=0 if /\\end{question}/;' "$file"
echo ""
done
done
echo "\end{document}"


If you run that script from the directory that contains Cardiology etc, it should provide output like this:

• How can you add the PATH to these directories. Those directories for instance locate at /Users/Masi/Dropbox/. I added a parameter like this path="/Users/Masi/Dropbox/ and for file in "$path/$directory"/*tex. I think something wrong in last line. – Léo Léopold Hertz 준영 Oct 5 '14 at 7:56
• @masi just write for file in /Users/Masi/Dropbox/"$directory"/*tex. But it should also have worked with the variable. – terdon Oct 5 '14 at 10:47 • See example output below. Duplicate sections only bug. I think the bug is in this line [ "$c" -eq 1 ] && printf "\n%s\n" "\section{\$directory}". – Léo Léopold Hertz 준영 Oct 5 '14 at 11:41
• @Masi yes indeed, sorry, silly mistake on my part. It should work as expected now. – terdon Oct 5 '14 at 15:17
• @Masi just save the script as foo.sh, make it executable (chmod 755 foo.sh) and run it redirecting its output: /path/to/foo.sh > /path/to/output.tex. – terdon Oct 5 '14 at 16:06