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.

Possible Duplicate:
Error using \usepackage{fancyvrb, fancybox}

I try to implement a simple environment to highlight source code in LaTeX. My solution is :

  1. use VerbatimOut to write text in a file
  2. use \write18 to execute source-hightlight to handle the saved file
  3. \input the result in the \shadowbox

And it works. The code looks like:

%%%
%%% A simple environment used to highlight source code
%%% Only usable in linux with source-highlight installed
%%% wwthunan@gmail.com
%%% 2012-07-05 Thu 16:00 PM CST+0800
%%%

\usepackage[usenames, dvipsnames]{color}
\usepackage{fancybox} % must placed before "fancyvrb"
\usepackage{fancyvrb}

\newcommand{\SourceTabWidth}{4}

\makeatletter
%% USAGE: 
%% \HandleCode{input}{output}{tabwidth}{source language}
\newcommand\HandleCode[4] {
    \immediate\write18{source-highlight --tab=#3 --src-lang=#4 --out-format=latexcolor -i #1 -o #2}
}

%% 
%% USAGE:
%% \begin{SourceCode}{language}
%%      ...code...
%% \end{SourceCode}
%% 
\newenvironment{SourceCode}[1] %
{ %
    \def\mySourceCodeLanguage{#1}
    \def\mySourceCodeInput{\jobname.code.#1}%
    \def\mySourceCodeOutput{\jobname.hcode.tex}%
    \VerbatimEnvironment%
    %% codes option to make sure TAB intact
    \begin{VerbatimOut}[codes={\catcode`\^^I=12}]{\mySourceCodeInput}}%
{\end{VerbatimOut}%
    \HandleCode{\mySourceCodeInput}{\mySourceCodeOutput}{\SourceTabWidth}{\mySourceCodeLanguage} %
    \shadowbox{%
        \parbox{0.8\textwidth}{%
        \input{\mySourceCodeOutput} %
        }%
    }%
    %% delete temp files 
    \immediate\write18{rm \mySourceCodeInput; rm \mySourceCodeOutput}%
}
\makeatother

However, there is a problem. When I put fancyvrb before fancybox package.

[codes={\catcode`\^^I=12}] 

option doesn't work, and a file [.tex will be created.

So I guess fancybox have changed some internel state. But I can't figure it out.

Any advice will be appreciated!

Save the code to source-highlight.sty. The sample code(sample.tex) is:

\documentclass{article}
\usepackage{source-highlight}
\begin{document}
\begin{SourceCode}{cpp}
int main(int argc, char* argv[]) {
    return 0;
}
\end{SourceCode}
\end{document}

And the compile command is:

pdflatex --shell-escape sample.tex
share|improve this question

marked as duplicate by egreg, tohecz, percusse, Martin Schröder, Marco Daniel Aug 16 '12 at 14:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Welcome to TeX.SX! Would you please complete your code into something compilable, adding a document class and an example of usage? –  egreg Jul 5 '12 at 9:09
    
The output seems to be as expected. As explained here the loading order should always be fancybox before fancyvrb. –  egreg Jul 5 '12 at 9:37
    
Got it. I found that there are so many restrictions and comflicts among the packages. –  Martin Wang Jul 5 '12 at 10:21
    
As I explained in the linked answer, fancybox wants to define VerbatimOut in a different way than fancyvrb, so loading the latter after the former ensures that the fancyvrb features work. You may want to use framed or mdframed instead of fancybox. –  egreg Jul 5 '12 at 10:29

1 Answer 1

up vote 2 down vote accepted

with current TeXLive 2011 I get the output:

enter image description here

I used the following code:

\RequirePackage{filecontents}
\begin{filecontents*}{source-highlight.sty}
\RequirePackage{xcolor}
\usepackage{fancybox} 
\usepackage{fancyvrb}
\newcommand{\SourceTabWidth}{4}
\newcommand\HandleCode[4] {
    \immediate\write18{source-highlight --tab=#3 --src-lang=#4 --out-format=latexcolor -i #1 -o #2}
}
\newenvironment{SourceCode}[1] %
{ \VerbatimEnvironment
    \def\mySourceCodeLanguage{#1}
    \def\mySourceCodeInput{\jobname.code.#1}%
    \def\mySourceCodeOutput{\jobname.hcode.tex}%
    \VerbatimEnvironment%0
\begin{VerbatimOut}[codes={\catcode`\^^I=12}]{\jobname.code.#1}}%
{\end{VerbatimOut}
  \HandleCode{\mySourceCodeInput}{\mySourceCodeOutput}{\SourceTabWidth}{\mySourceCodeLanguage} %
    \shadowbox{%
        \parbox{0.8\textwidth}{%
        \input{\mySourceCodeOutput} %
        }%
    }%
    \immediate\write18{rm \mySourceCodeInput; rm \mySourceCodeOutput}%
}
\endinput
\end{filecontents*}

\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}
\documentclass{beamer}
\usepackage{source-highlight}
\begin{document}
\begin{frame}[fragile]{Hihlight demo}
foo
\begin{SourceCode}{cpp}
int main(int argc, char* argv[]) {
    return 0;
}
\end{SourceCode}
bar
\end{frame}
\end{document}
share|improve this answer
    
Thanks for trying. The code is too problematic to use. As far as I know, it conflict with beamer because of reload of color package, and the TABs are gone. For xelatex, codes option is redundant. –  Martin Wang Jul 5 '12 at 10:25
    
for beamer use \PassOptionsToPackage, see my edited answer. it now uses beamer. My tabs are not gone ... –  Herbert Jul 5 '12 at 10:32
    
Nice, works for both pdfltex and xelatex. –  Martin Wang Jul 5 '12 at 10:38

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