My general problem is the following. I want to "convert" many .tex files into some other format (xml-like). I don't want to modify the tex files, I can only build a documentclass. The texfiles are organized in blocs ( i will call them content-blocs), through commands and environment, for example, roughly:
\begin{document}
\content{blablabla}
\titre{joli titre}
\explanation{i muss explain that...}
\begin{definition}
here some nice text
\end{definition}
....
\end{document}
My present method, is to capture the content of the commands or environments and use \write
to write them in a file:
\newwrite\tempfile
\immediate\openout\tempfile="test.xml"
For example (the following is defined in the documentclass used by the tex file):
\newcommand{\content}[1]{
\immediate\write\tempfile{<content>}
\immediate\write\tempfile{\content}
\immediate\write\tempfile{</content>}
}
The complication comes from the fact, that the commands and environments in \content
may contain more than text, e.g other macros. For example :
\content{
some blablabla \textbf{important} etc etc
}
\begin{definition}
consider the following points :
\begin{quote}
quote quote quote
\end{quote}
and so on, and so on...
\end{definition}
In this form, this gives a compilation error, because \write
can't write this into the file. On the other hand, I know the exhaustive list of commands/environments that can appear in the "content-blocs". I though about capturing them, through a redefinition. For example :
\renewcommand{\textbf}[1]{<bold>#1</bold>}
works, the command \content
will produces in the file "test.xml" a line
some blablabla <bold>important</bold> etc etc
Problem 1: This strategy does not work for environments. My attempt
\renewenvironment{quote}{<quote>}{</quote>}
fails miserably.
Problem 2: I would need this in particular for the itemize
environment (instead of quote
). I know that redefining \item
may yield some problems
Problem 3: In the content-blocks, I have sometimes commands with optional arguments, eg :
\content{bla bla \com[opt]{AAA} bla bla}
Here again, it fails, because commands with optional arguments are not fully expandable. As workaround, i can redefine the command \com
with \DeclareExpandableDocumentCommand
from the package xparse
.
\DeclareExpandableDocumentCommand\expandCom{ o m} {<com : #1>#2</com>}
It seems to solve my problem for commands with optional arguments, but I would need the same for environments with optional arguments.
Problem 4: I need to treat the content of the content-blocs, in order to extract information. For this i use in particular \edef
, and i need that the macros (\com
, \texbf
, and so on...) expand correctly in a edef
. But i guess that it is exactely the same problem like with \write
. For both \edef
and \write
one needs fully expandable macros inside, isn't it ?
EDIT1 Concerning Bruno's question. I think that David's solution is somehow secure, because it does not redefine the existing commands/environments, but it rather creates new ones (\mwx\begin
, etc...).
Nevertheless, following Bruno's suggestion, i try to simply redefine the command \begin
and \end
for the environments i need (quote and definition, in this example). My attempt fails nevertheless. It might be because of \ifthenelse
, but on the other hand, i never feel comfortable with \ifx
:
\documentclass{article}
\usepackage{ifthen}
\let\originalbegin\begin
\let\originalend\end
\def\begin#1{%
\ifthenelse{\equal{#1}{quote}}{<quote:special def for quote>}{
\ifthenelse{\equal{#1}{definition}}{<DEF: another definition>}{\originalbegin{#1}
}
}
}
\def\end#1{%
\ifthenelse{\equal{#1}{quote}}{</quote>}{
\ifthenelse{\equal{#1}{definition}}{</DEF>}{\originalend{#1}
}
}
}
\newwrite\tempfile
\immediate\openout\tempfile="nico.xml"
\begin{document}
\def\content{
some blablabla etc etc
\begin{definition}
consider the following points :
\begin{quote}
quote quote quote
\end{quote}
and so on, and so on...
\end{definition}
}
\immediate\write\tempfile{\content}%
\immediate\closeout\tempfile
\end{document}
TeX
solution? Second, you might get more useful help if you provided a MWE with inputTeX
file and a handbuilt file showing the output you wanted.