How do I create a new Beamer environment with a verbatim environment? - latex

I'm creating a Beamer presentation that has a lot of example LaTeX in it, which has to go in a verbatim environment. I'm getting tired of typing
\begin{example}
\begin{verbatim}
Verbatim Text
\end{verbatim}
\end{example}
I wish to create a new command or environment that will shorthand this for me. I also need this for blocks and theorems, since I'm using those frequently as well. But if I can figure it out for examples, it should be easy to translate to another example.
I can't create a new environment or command using just \begin{verbatim}, since it cuts off the rest of the command. So I switched to using the fancyvrb package, and tried the following:
\DefineVerbatimEnvironment
{MyVerbatim}{Verbatim}{}
\newcommand{\makeexample}[1]{
\begin{example}
\begin{MyVerbatim}
#1
\end{MyVerbatim}
\end{example}
}
\makeenvironment{VerbExample}{\begin{example}
\begin{MyVerbatim}}{\end{MyVerbatim}\end{example}}
That gives me the \makeexample{Example Text} command, and the \begin{VerbExample}...\end{VerbExample} environment, but they both still throw errors on compile. The frame I'm trying to use them in looks like this (I've got the [fragile] option on the frame, so it's not that).
\begin{frame}[fragile]
\frametitle{Why Doesn't Verbatim Work?}
\makeexample{Verbatim Text}
\begin{VerbExample}
Verbatim Text
\end{VerbExample}
\end{frame}

Environment definition:
\newenvironment{VerbExample}
{\example\semiverbatim}
{\endsemiverbatim\endexample}
Frame definition:
\begin{frame}[fragile]
\frametitle{Title}
\begin{VerbExample}
test test test $t$ $\\omega$
test test
\end{VerbExample}
\end{frame}
Verbatim cannot go inside \newcommand. Semiverbatim is defined by Beamer and works well with it. The three characters \ { } must be escaped as \\ \{ \}.
Source: Beamer user guide, pp. 119-120
http://www.ctan.org/tex-archive/macros/latex/contrib/beamer/doc/beameruserguide.pdf

Related

How can I tweak the numbering inside the appendices environment?

I have the following structure in my Latex book:
1) main file
...
\include{chapter1}
\include{chapter2}
\include{chapter3}
...
2) chapterN.tex
\input{file1}
\input{file2}
\input{file3}
\begin{appendices}
\input{appendix_1_chapterN}
\input{appendix_2_chapterN}
\end{appendices}
The result is that the appendixes at the end of chapter 1 (for instance) is named ".1" and ".2", when I would like them to be name "1.A" and "1.B".
Any help is appreciated.
The answer to this is covered in the documentation to the appendix package, which is required for the appendices environment that you are using: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/appendix/appendix.pdf
The answer to your question is that you are using the appendices environment for section appendices, where it is designed for chapter appendices. To achieve your goal of section appendices, use the subappendices environment instead as shown below:
\documentclass{book}
\usepackage{appendix}
\begin{document}
\chapter{Intro}
\section{Intro Sec}
\begin{subappendices}
\section{Appendix Sec}
\end{subappendices}
Some Text
\chapter{Conclusion}
Some Text
\end{document}

Problem creating a lstnewenvironment that starts/ends another environment

I am currently using Beamer and the listing package to pretty-print code into Beamer blocks. So what I'm doing looks like :
\begin{block}{}
\begin{lstlisting}
int foobar(void) { return 0; }
\end{lstlisting}
\end{block}
Now, I find it cumbersome to start the block and lstlisting environments everytime. I'd like to have a simple codeblock environment that just does it:
\begin{codeblock}
int foobar(void) { return 0; }
\end{codeblock}
So, I tried something like :
\lstnewenvironment{codeblock}
{\begin{block}{}}
{\end{block}}
But unfortunately, the Beamer document no longer compiles, with the following error:
! Missing } inserted.
<inserted text>
}
l.178 \end{frame}
?
Is there some way to do this ?
In Problem with creating a newenvironment in LaTeX, Andreas Grech had the same problem, but it could solve it since there was another way to enter/exit the enclosing environment. But in the case of the block Beamer environment, it seems there is no other way than doing \begin{block}...\end{block}.
I had the same problem and could not find a solution for it. My workaround was to use the \lstinputlisting command and have the code in a separate file. That's great if you have real code you want to include. Not so for small examples.
Another workaround is to put the code snipplet into a variable before starting the {frame} environment and then reference it. How to do this is explained in latex-beamer docs. It would also allow you to employ your custom environment/command.
I "solved" this by using the fancyvrb package's \VerbatimOut(See write environmnet body verbatim to a file) to create a temporary file which then can be included with lstinputlisting:
\usepackage{fancyvrb}
\usepackage{listings}
\newenvironment{blocklisting}[1]
{\begingroup\lstset{#1}\VerbatimOut{blocklisting-tmp.txt}}
{\endVerbatimOut\begin{block}{Code}\lstinputlisting{blocklisting-tmp.txt}\end{block}\endgroup}
For some reason i could not make the environment-argument optional, though.
Used like this:
\begin{document}
\begin{frame}[fragile]
\frametitle{Whatever}
\begin{blocklisting}{language=Java, basicstyle=\Huge}
Code
\end{blocklisting}
\begin{blocklisting}{}
Code 2
\end{blocklisting}
\end{frame}
\end{document}
Not the optimal solution, but it works, i guess.

LaTeX semiverbatim and fancyvrb

I'm creating a presentation using the beamer LaTex package. Beamer
comes with an environment called "semiverbatim" which is like
"verbatim", but allows you to place commands inside the environment.
This is useful in beamer to control how the overlays of a frame
unfold. For example:
\begin{frame}[fragile, shrink]
\frametitle{Some Code Sample}
\begin{semiverbatim}
private String foobar() \{
String s = "val"
\alert<2->{s = null};}
return s;
\}
\end{semiverbatim}
\end{frame}
This will cause the third line to appear red in the second stage of
the frame transition.
This is all good and fine, however, the "semiverbatim" environment,
much like the "verbatim" environment, is pretty limited. I would like
to use the "Verbatim" environment from the fancyvrb package.
Is there anyway to use "Verbatim" in the same way "semiverbatim" is
used?
I'm not having much luck, I'm afraid. I can get the \alert to work okay, but only without an overlay specification:
\documentclass{beamer}
\usepackage{fancyvrb}
\begin{document}
\begin{frame}[fragile]
\frametitle{Some Code Sample}
\begin{Verbatim}[commandchars={\\[]}]
private String foobar() {
String s = "val"
\alert[s = null];}
return s;
}
\end{Verbatim}
\end{frame}
\end{document}
When you try \alert<2-> it breaks, and changing catcodes of < and > doesn't seem to help.
Not sure if it helps you directly, but when I've loaded source into a beamer slide, I used the listings package, lstset, and the lstlisting environment. I never use any reveals in the code, though, so I haven't tested that interaction.

Latex new command

I would like to define a new command like this
\newcommand{\bob}[1]{\excerpt \begin{lstlisting} {#1} \end{lstlisting}}
and then use it like this
\bob{abcd}
but i get the following error in latex.
text dropped after begin of listing latex
EDIT:
I tried the following
\newcommand{\boy}[1] {{%
\begin{Verbatim} %
{ #1 } %
\end{Verbatim} }}
And I still get an error when I try to use it.
\boy{abc}
Perhaps you are looking for the \newenvironment macro.
In this case you would use it like this
\newenvironment{bob}{%
\excerpt \begin{lstlisting}}{%
\end{lstlisting}}
and later
\begin{bob}
abcd
\end{bob}
The {listing} environment is special and magical; it can't be used inside a command like that. Changing to a \newenvironment setup as described by dmckee should work. If you can't make that work, check out the fancyvrb package.
Try the lstnewenvironment of the listings package.

header width on the last page of the chapter

I'm trying to turn off marginpar when starting a new multicols environment with this new environment, which uses the multicols and chngpage packages:
\newenvironment{multi}[1]{%
\newlength{\newtextwidth}%
\setlength{\newtextwidth}{\marginparwidth}%
\addtolength{\newtextwidth}{-1cm}%
\addtolength{\headheight}{.5cm}%
\let\oldheadrule\headrule%
\addtolength{\headwidth}{\newtextwidth}%
\begin{adjustwidth}{}{-\newtextwidth}\begin{multicols}{#1}}%
{\end{multicols}\end{adjustwidth}}
Which works great:
latex header http://img6.imageshack.us/img6/6757/screenshotewa.png
Uhm, almost, since on the last page of the current chapter "Lorem ipsum" it behaves like I hadn't instruct it to: \addtolength{\headwidth}{\newtextwidth}:
latex header at the end of the chapter http://img11.imageshack.us/img11/6072/screenshotwbd.png
How could I fix that?
Edit:
I'm also using fancyhdr.
2nd Edit:
A PoC:
\documentclass[12pt,a4paper,oneside]{report}
\usepackage[utf8]{inputenc}
\usepackage[top=2cm,left=2cm,right=4.5cm]{geometry}
\usepackage{chngpage}
\usepackage{color}
\usepackage{amsmath}
\usepackage[pdftex,bookmarks,pdfpagemode=UseOutlines,bookmarksopen,backref
,colorlinks,urlcolor=blue,linktocpage]{hyperref}
\usepackage{url}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage{indentfirst}
\usepackage{listings}
\usepackage{boxedminipage}
\pagestyle{fancy}
\setlength{\columnseprule}{1pt}
\setlength{\marginparwidth}{4cm}
\rhead{\large\leftmark}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\makeatletter
\renewcommand*\#makechapterhead[1]{%
{\parindent \z# \raggedright \normalfont
\huge\bfseries
#1\par\nobreak
\vskip 20\p#
}}
\makeatother
\let\oldmarginpar\marginpar
\renewcommand\marginpar[1]{\-\oldmarginpar[\sffamily\raggedleft\footnotesize #1]%
{\sffamily\raggedright\footnotesize
\begin{boxedminipage}{\marginparwidth}#1\end{boxedminipage}
}}
\newenvironment{multi}[1]{%
\newlength{\newtextwidth}%
\setlength{\newtextwidth}{\marginparwidth}%
\addtolength{\newtextwidth}{-1cm}%
\addtolength{\headheight}{.5cm}%
\let\oldheadrule\headrule%
\addtolength{\headwidth}{\newtextwidth}%
\begin{adjustwidth}{}{-\newtextwidth}\begin{multicols}{#1}}%
{\end{multicols}\end{adjustwidth}}
\begin{document}
\tableofcontents
\chapter{Lorem ipsum}
\begin{multi}{2}
\lipsum[1-20]
\end{multi}
\chapter{Lorem ipsum}
\begin{multi}{2}
\lipsum[1-20]
\end{multi}
\chapter{Lorem ipsum}
\begin{multi}{2}
\lipsum[1-20]
\end{multi}
\end{document}
It should be possible to continue single-column on the same page, after "multi", but the headers must be kept like when the page was started within the "multi" environment.
Why would I need single-column after multi-column on the same page with a marginpar? Imagine presenting the source code for the article, with small hints on the margin. (That's what the listing package is there for)
I suspect your last heading box is being constructed after your text finishes making the multicol boxes, so you're out of the scope of your change. It goes back to the old value.
You'd probably do well to add the fancyhdr package and use it. I believe it's well-behaved in multicolumn.
Okay, so it's almost certainly the scope thing. You're doing the adjustwidth in your new multi environment. When your text runs out in the multi envirnment, you haven't filled the last page; headers aren't set up until the page is filled. So your mutlti environment finishes the box, you leave the scope, and THEN the page is finished and emitted. Using the old width.
Set the header width and parameters outside the environment.
I ran your test document. It seems to have a bug in that \newlength{\newtextwidth} has a global effect, and so causes an error. Not sure why that is but I pulled it out of the \newenvironment{multi} with no ill effect.
Charlie's diagnosis is definitely correct. An alternative solution is to end the page after the multicols but before the adjustwidth, thus:
\newenvironment{multi}[1]{%
...}
{\end{multicols}\vfill\break\end{adjustwidth}}
I have tested this solution and on your sample document, it produces good output.

Resources