I'm making Beamer presentation. How I can display LaTeX code inside presentation?
Asked
Active
Viewed 1.4k times
13
-
Duplicate? http://tex.stackexchange.com/questions/867/syntax-coloring-in-latex – Martin Tapankov Oct 14 '10 at 06:31
3 Answers
24
Either simply within the verbatim environment or more sophisticated with the listings package.
Thorsten Donig
- 46,014
-
16Remember that any frames containing a verbatim or lstlisting material must be passed the 'fragile' parameter, i.e. \begin{frame}[fragile] .... \end{frame} – imnothere Oct 14 '10 at 06:52
-
4And don't get scared when you get a "file ended while scanning" error, it means that the \end{frame} of a fragile frame is not at the beginning of a line (but should be). – Ulrich Schwarz Dec 04 '10 at 22:04
11
If you want the input LaTeX codes displayed together with their corresponding outputs side by side (left/right or top/bottom), use showexpl.sty. It is a really cool package.
There are 2 modes:
- the input code is inserted into your document, or
- the input code is imported from external file.
EDIT 1:
The following figure shows showexpl.sty in action:

And the corresponding code is given as follows: Note: I gave some comments in the code to make it clearer.
\documentclass[final,dvipsnames]{article}
\usepackage[hmargin=70mm]{geometry}
\usepackage{xcolor}
\usepackage{bera}
\usepackage{pstricks-add}
\usepackage{caption}
\captionsetup{%
margin=0em,
font={scriptsize,rm},
labelfont={color=Maroon,bf},
justification=justified,%default: RaggedRight. Other options: justified, centering
labelsep=quad%default:colon. Options: period, space, quad, newline
}
\usepackage{showexpl}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\SX@codeInput}{xleftmargin=0pt,xrightmargin=0pt}{}
{\typeout{***Successfully patched \protect\SX@codeInput***}}
{\typeout{***ERROR! Failed to patch \protect\SX@codeInput***}}
\makeatother
\lstset{%
breaklines=true,%default : false
breakindent=10pt,%default: 20pt
linewidth=\linewidth,%default : \linewidth,
basicstyle=\ttfamily\tiny,% cannot take arguments
keywordstyle=\color{Blue}\sffamily\bfseries,
identifierstyle=\color{Black},
commentstyle=\color{OliveGreen}\itshape,
stringstyle=\rmfamily,
showspaces=false,%default false
showstringspaces=false,%default: true
backgroundcolor=\color{Yellow!30},
frame=single,%default frame=none
rulecolor=\color{Red},
% the following must be defined to make hacking work.
framerule=0.4pt,%expand outward
framesep=3pt,%expand outward
xleftmargin=3.4pt,%to make the frame fits in the text area.
xrightmargin=3.4pt,%to make the frame fits in the text area.
tabsize=2%,%default :8 only influence the lstlisting and lstinline.
}
\lstset{%
%explpreset below will affect LTX only.
explpreset={%
language={[LaTeX]TeX},
captionpos=t,
aboveskip=2\fboxsep,
pos=b,
vsep=\fboxsep%vertical space between formatted and input.
}%
}
\newcommand\mywidth{0.5\linewidth}
\newcommand\dummy{%
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
}
\begin{document}
\begin{LTXexample}[caption=\dummy]
\dummy
\begin{enumerate}
\item $\displaystyle \lim_{x\to 8}\frac{1}{(x-8)^2}=+\;\rotatebox{90}{$8$}$
\item $\displaystyle \lim_{x\to 7}\frac{1}{(x-7)^2}=+\;\rotatebox{90}{$7$}$
\item $\displaystyle \lim_{x\to 6}\frac{1}{(x-6)^2}=+\;\rotatebox{90}{$6$}$
\end{enumerate}
\end{LTXexample}
\end{document}
Display Name
- 46,933
9
How about to use verbatiminput? If you have the LaTeX examples in a file?
\usepackage{verbatim}
\verbatiminput{ my_test.tex }
Johan
- 2,204