6

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)?

7

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.

  • Works great, using it with \includedoc*{rtf2tex} so I do not have to remove the \document entries. Just a small issue left: Can I somehow trim whitespaces at the start of the data with \VerbatimOut (file needs to start exactly with '{\rtf...')? – Stefan Mar 8 '12 at 17:58
  • This is a very cool use of the rtf2latex2e program. I think that I will link to it from the documentation on the rtf2latex2e.sf.net website. – Scott Prahl Mar 25 '12 at 6:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.