[Tex/LaTex] Dynamically use RTF-Code in latex-document

environmentsformattingtools

I'm looking for a way to dynamically use Rich-Text-Format code in a latex document.

The background is this: I want to paste formatted programming-source from visual studio into my latex code because the ability of VS to format code is way above any latex package, as it can colour self defined class names according to their definitions. VS supports this by copying RTF-text to the clipboard.

I would now like to insert this formatted code in latex without having to process every code snippet manually. I was hoping for something like this:

\begin{rtf-code}
    {\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19            \cf1 foreach\cf0  (\cf1 var\cf0  fsf \cf1 in\cf0  fsFiles)\par          \{\par              \cf1 if\cf0  (System.IO.\cf2 Path\cf0 .GetExtension(fsf).Equals(\cf3 ".txt"\cf0 , \cf2 StringComparison\cf0 .InvariantCultureIgnoreCase))\par               \{\par                  \cf2 Console\cf0 .WriteLine(\cf3 "Importing routes data from '\{0\}'..."\cf0 , fsf);\par                    ebdImp.ImportData(fsf, \cf3 ""\cf0 );\par               \}\par          \}\par }
\end{rtf-code}

For the transformation of the code itself I found the RTF to LaTeX conversion tool rtf2latex2e.

So I am now looking for a way to embed this tool dynamically into my latex build. The goal is to define an environment (or command or whatever) that does the following:

  1. Export the enclosed code to a temporary file
  2. Run rtf2latex2e over it (shell execute)
  3. Import the result file to be further treated by latex in place of the rtf-code

Is something like this possible at all? And can someone give me a hint where to start (don't care about the tool itself, it's open source and I will modify it to produce correct latex code)?

Best Answer

The solution consists essentially in writing

\usepackage{fancyvrb,standalone}
\newenvironment{rtf-code}
  {\VerbatimOut{temporary.rtf}}
  {\endVerbatimOut\immediate\write18{rtf2latex2e temporary.rtf}%
   \input{temporary}}

and calling pdflatex -shell-escape.

Related Question