Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show LaTeX-code in a LaTeX document?

Tags:

latex

I have a little problem where I would like to insert a svn diff of a LaTeX document into another LaTeX document, the goal is to show what has changed since revision XXX. However since the diff contains a lot of LaTeX command I can't include it right into the document since LaTeX will interpit them and not just "print" them.

Today I have this in my Makefile

DIFF_INFO=diff.info.tex
DIFF_REV=167
diffinfo:
    $(shell echo "\n" > $(DIFF_INFO) )
    $(shell echo "\\section{diff $(DIFF_REV)} \n" >> $(DIFF_INFO) )
    $(shell echo \\\\begin{verbatim} >> $(DIFF_INFO) )
    $(shell svn diff --revision $(DIFF_REV) $(N).tex >> $(DIFF_INFO) )
    $(shell echo \\\\end{verbatim} >> $(DIFF_INFO) )

And at the end of the LaTeX document I have this:

\IfFileExists{diff.info.tex}
{
  \newpage
  \input{diff.info.tex}
}

But this fails hard!

My next idea is to write a perl script that replaces all invalid chars with something that LaTeX can show, but it feels like I'm risking to reinvent the wheel so I figured that I could ask if someone had a better idea?

How do I include and show LaTeX code in the document?

Thanks Johan


Update: Thanks "unknown (google)" for pointing out verbatim, it did what I wanted it to.

Update: I also looks like I should try that listings that las3rjock told us about since it look kind of nice.

Update: Could not get listings to work in my case, I get some strange utf warnings about invalid chars. But the verbatim is working so I will use that way this time.

like image 315
Johan Avatar asked Sep 06 '09 18:09

Johan


2 Answers

I second Boojum's recommendation in a comment to another answer that you use the listings package. For LaTeX code listings, I use settings that I found in André Miede's classicthesis package. Here is a sample document (quine.tex) and its output:

\documentclass[12pt,letterpaper]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

% listings settings from classicthesis package by
% Andr\'{e} Miede
\lstset{language=[LaTeX]Tex,%C++,
    keywordstyle=\color{RoyalBlue},%\bfseries,
    basicstyle=\small\ttfamily,
    %identifierstyle=\color{NavyBlue},
    commentstyle=\color{Green}\ttfamily,
    stringstyle=\rmfamily,
    numbers=none,%left,%
    numberstyle=\scriptsize,%\tiny
    stepnumber=5,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=single
    %frame=L
}

\begin{document}
\lstinputlisting{quine.tex}
\end{document}

LaTeX document displaying its own source code
(click to enlarge)

like image 128
las3rjock Avatar answered Oct 28 '22 08:10

las3rjock


There' a verbatim package that you can include with \usepackage{verbatim} and access using \verbatiminput{filename}.

like image 40
user57368 Avatar answered Oct 28 '22 09:10

user57368