Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

    {\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 }

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

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The solution consists essentially in writing

  {\endVerbatimOut\immediate\write18{rtf2latex2e temporary.rtf}%

and calling pdflatex -shell-escape.

share|improve this answer
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
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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