[Tex/LaTex] How to convert structured LaTeX file *to* CSV data

databasedatatoolforms

I am using a structured format to print patient's documenatation using LaTeX. The structure is like this:

Demography
Name Surname
Age/Sex
Address

Clinical Details
History, Past History, Personal History, Family History and others (as subsections) er:

Examination

Diagnosis
Provisional
Final

Investigations
available and suggested

Treatment

I used koma article class (scrartcl) with section, subsection, enumerate. I am aware of the datatool package which I could have used to get the things done, while I would have documented the visit to csv/excel/calc. I could not figure out how a csv entries could be filtered so that I could print an individual patient record.
My question is, if I follow above mentioned simple structure, is there any way to export the data of all patient to csv file/excel/calc for analysis?

I am typing the structure of tex file :

\documentclass[12pts]{scrartcl}
\usepackage{fontspec}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=1cm,rmargin=1cm}
\usepackage{array}
\usepackage{fixltx2e}
\setmainfont[Script=Devanagari]{Sanskrit 2003}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\usepackage{enumitem}       % customizable list environments
\newlength{\lyxlabelwidth}      % auxiliary length 
\@ifundefined{lettrine}{\usepackage{lettrine}}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.

\usepackage[us,24hr]{datetime}

\makeatother

\usepackage{xunicode}
\usepackage{polyglossia}
\title{Consultation Report}
\begin{document}
\maketitle

\section*{Clinical details}

\subsection*{Present history}

\subsection*{Past History}

\subsection*{Personal history}

\subsection*{Family history }

\subsection*{Examination}
examination finding 
\section*{Diagnosis}
\begin{enumerate}
\item  Disease
\end{enumerate}

\section*{Rx}
\begin{enumerate}
\item here goes teatment
\item tab abc दिवसातून दोनदा 
\end{enumerate}
\begin{flushright}
My Name \\
{\footnotesize Qualification
}\\
{\footnotesize designation}\\
{\footnotesize Ph: }
\par\end{flushright}{\footnotesize \par}

\end{document}

I think I need to elaborate. Till date I was using the template provided to create a regular consultation. The receptionist used to create a folder named after the patient and insert a demography.tex file containing

Name surname \\\ dob\\\address\\\Phone.

I used to create small tex files named as yymmdd.tex where I used to enter clinical data. Clinical data included history, examination and diagnosis.
We use to create a third small file Rx.tex where itemised treatment used to go. The three files used to be compiled by placing \input commands in a template provided. The advantage of having three small files was I could use demography in a follow up visit. Treatment mostly had to be modified a little bit. The clinical data used to change, so I had to change the input file everytime in a template. Overall, the text generated used to have consistent patern. I could share the folders on intranet. But possibly there is better way of doing this using data tool. I had two problems using data tool.

  1. Though I understood how to create tables using data tool, I failed to understand how an individual entry can be used. E.g. presenting history for presenting history in already provided template, past history at past history template.

  2. If I use unique identification no for every patient and visit, how can I export only the data for the visit day to latex template.

If I can figure it out, then may be all my patient data can be entered on excel and exported to latex templates to create a needed document. The question, why not use a MySQL like database? It seems beyond my capacity and available time to learn new tool, though it may be worth it. In no way I am creating emr. That is way beyond. If you can help me out in getting cvs to LaTeX template I provided I will be obliged. For technical reason I would like | to be separator and not the comma.

Best Answer

I agree with the comments that you'd need a scripting language to extract the information from your existing documents, but once you have the information in a database you can use the datatool package to generate a report for a particular patient. For the type of information you want to save, I wouldn't recommend using csv as I would find that tricky to edit (but that depends on what you use to edit it). Here's how it might look in csv form (using | as a separator as you requested):

Patient Number|Name|Surname|DoB|Sex|Address|Phone|History|Past History|Personal History|Family History|Examination|Diagnosis|Treatment
0001|Joe|Bloggs|1974-12-06|Male|"1 The Street, The Town"|0123456|Joe's patient history here|Joe's past history here|Joe's personal history|Joe's family history|Joe's examination|\begin{enumerate}\item Joe's diagnosis\end{enumerate}|\begin{enumerate}\item Joe's treatment\end{enumerate}
0002|Jane|Doe|1970-05-18|Female|"2 The Street, The Town"|0123457|Mary's patient history here|Mary's past history here|Mary's personal history|Mary's family history|Mary's examination|\begin{enumerate}\item Mary's diagnosis\end{enumerate}|\begin{enumerate}\item Mary's treatment\end{enumerate}
0003|John|Smith|1969-01-20|Male|"3 The Street, The Town"|01234568|John's patient history here|John's past history here|John's personal history|John's family history|John's examination|\begin{enumerate}\item John's diagnosis\end{enumerate}|\begin{enumerate}\item John's treatment\end{enumerate}

Alternatively, the same information can be store in a .tex file using the datatool format:

\DTLnewdb{patients}
% Patient Joe Bloggs (patient number 0001)
\DTLnewrow*{patients}
\DTLnewdbentry*{patients}{Patient Number}{0001}
\DTLnewdbentry*{patients}{Name}{Joe}
\DTLnewdbentry*{patients}{Surname}{Bloggs}
\DTLnewdbentry*{patients}{DoB}{1974-12-06}
\DTLnewdbentry*{patients}{Sex}{Male}
\DTLnewdbentry*{patients}{Address}{1 The Street, The Town}
\DTLnewdbentry*{patients}{Phone}{0123456}
\DTLnewdbentry*{patients}{History}{Joe's patient history here}
\DTLnewdbentry*{patients}{Past History}{Joe's past history here}
\DTLnewdbentry*{patients}{Personal History}{Joe's personal history}
\DTLnewdbentry*{patients}{Family History}{Joe's family history}
\DTLnewdbentry*{patients}{Examination}{Joe's examination}
\DTLnewdbentry*{patients}{Diagnosis}
{%
  \begin{enumerate}
    \item Joe's diagnosis
  \end{enumerate}
}
\DTLnewdbentry*{patients}{Treatment}
{%
  \begin{enumerate}
    \item Joe's treatment
  \end{enumerate}
}
% Patient Jane Doe (patient number 0002)
\DTLnewrow*{patients}
\DTLnewdbentry*{patients}{Patient Number}{0002}
\DTLnewdbentry*{patients}{Name}{Jane}
\DTLnewdbentry*{patients}{Surname}{Doe}
\DTLnewdbentry*{patients}{DoB}{1970-05-18}
\DTLnewdbentry*{patients}{Sex}{Female}
\DTLnewdbentry*{patients}{Address}{2 The Street, The Town}
\DTLnewdbentry*{patients}{Phone}{0123457}
\DTLnewdbentry*{patients}{History}{Mary's patient history here}
\DTLnewdbentry*{patients}{Past History}{Mary's past history here}
\DTLnewdbentry*{patients}{Personal History}{Mary's personal history}
\DTLnewdbentry*{patients}{Family History}{Mary's family history}
\DTLnewdbentry*{patients}{Examination}{Mary's examination}
\DTLnewdbentry*{patients}{Diagnosis}
{%
  \begin{enumerate}
    \item Mary's diagnosis
  \end{enumerate}
}
\DTLnewdbentry*{patients}{Treatment}
{%
  \begin{enumerate}
    \item Mary's treatment
  \end{enumerate}
}
% Patient John Smith (patient number 0003)
\DTLnewrow*{patients}
\DTLnewdbentry*{patients}{Patient Number}{0003}
\DTLnewdbentry*{patients}{Name}{John}
\DTLnewdbentry*{patients}{Surname}{Smith}
\DTLnewdbentry*{patients}{DoB}{1969-01-20}
\DTLnewdbentry*{patients}{Sex}{Male}
\DTLnewdbentry*{patients}{Address}{3 The Street, The Town}
\DTLnewdbentry*{patients}{Phone}{0123458}
\DTLnewdbentry*{patients}{History}{John's patient history here}
\DTLnewdbentry*{patients}{Past History}{John's past history here}
\DTLnewdbentry*{patients}{Personal History}{John's personal history}
\DTLnewdbentry*{patients}{Family History}{John's family history}
\DTLnewdbentry*{patients}{Examination}{John's examination}
\DTLnewdbentry*{patients}{Diagnosis}
{%
  \begin{enumerate}
    \item John's diagnosis
  \end{enumerate}
}
\DTLnewdbentry*{patients}{Treatment}
{%
  \begin{enumerate}
    \item John's treatment
  \end{enumerate}
}

You can access a particular patient like this:

\documentclass{scrartcl}

\usepackage{datatool}

% load from csv file:
%\DTLsetseparator{|}
%\DTLloaddb{patients}{patients.csv}
% or load from .tex file:
\input{patients}

\title{Consultation Report}
\author{}

\newcommand*{\getdetails}[1]{%
  \dtlgetentryfromcurrentrow{\patientdetails}{\dtlcolumnindex{patients}{#1}}%
  \patientdetails
}

\begin{document}
\maketitle

% fetch patient's details (patient number 0002)
\dtlgetrowforvalue{patients}{\dtlcolumnindex{patients}{Patient Number}}{0002}%

\begin{tabular}{ll}
Name: & \getdetails{Name} \getdetails{Surname}\\
DoB: & \getdetails{DoB}\\
Sex: & \getdetails{Sex}\\
Address: & \getdetails{Address}\\
Phone: & \getdetails{Phone}
\end{tabular}

\section*{Clinical details}

\subsection*{Present history}

\getdetails{History}

\subsection*{Past History}

\getdetails{Past History}

\subsection*{Personal history}

\getdetails{Personal History}

\subsection*{Family history }

\getdetails{Family History}

\subsection*{Examination}

\getdetails{Examination}

\section*{Diagnosis}

\getdetails{Diagnosis}

\section*{Rx}

\getdetails{Treatment}

\begin{flushright}
My Name \\
{\footnotesize Qualification
}\\
{\footnotesize designation}\\
{\footnotesize Ph: }
\par\end{flushright}{\footnotesize \par}

\end{document}
Related Question