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.
Related
When using:
\begin{listing}
...
\caption{foo}
\end{listing}
The caption will say: Listing x: foo. How can I replace the word Listing with something else?
If you are using minted (I am, and my source looks like yours), you may want to try
\renewcommand{\listingscaption}{Some fancy listing}
You might want to read the manual
http://mirror.switch.ch/ftp/mirror/tex/macros/latex/contrib/listings/listings.pdf
page 32
\begin{listing}[caption=Some fancy listing]
or try
\begin{listing}[title=Some fancy listing]
or try
\renewcommand{\lstlistingname}{A funny listing}
Minimal example that works for me:
\documentclass{article}
\usepackage{listings}
\renewcommand{\lstlistingname}{Something}
\begin{document}
Some text.
\begin{lstlisting}[caption=wwww]
xxxx
\end{lstlisting}
Some more text.
\end{document}
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.
Is there an easy way to have the complete code in a lstlisting environment underlined?
My current solution looks like this, but I'm not really happy with it.
\begin{lstlisting}[mathescape]
$\ul{if(gt(x1, 0)) then} $
...
\end{lstlisting}
Thanks for any tips.
According to page 5 in the user guide (found here):
\lstset{keywordstyle=\underbar}
If you want to underline the entire line (and not only the keywords), the best solution I can come up with is to do something along the lines below:
\usepackage{listings}
\newcommand{\lstul}[1]{\underline{\mbox{\tt #1}}}
\begin{document}
\begin{lstlisting}[mathescape]
$\lstul{if condition}$
$\lstul{statement 1}$
$\lstul{statement 2}$
...
\end{lstlisting}
\end{document}
I am trying to implement this new environment in LaTeX:
\newenvironment{javacode}[2]
{\begin{lstlisting}[language=java, label=#1, caption=#2]}
{\end{lstlisting}}
And then use it like such:
\begin{javacode}{c}{some code}
int x = 5;
\end{javacode}
But I am getting the following error:
Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][]
[1] [2]) [3])
*
Can anyone help as regards fixing this problem?
[Update]
I tried it doing it like Red-nosed unicorn instructed, and it worked correctly.
But now I tried adding a \begin{singlespace} like such:
\lstnewenvironment{javacode}[2]
{
\begin{singlespace}
\lstset{language=java, label=#1, caption=#2}}
{
\end{singlespace}
}
And I got the same error:
Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][]
[1]) [2] [3])
*
This is a special case because the listings environment needs to parse ahead itself to find the end of itself. The reason is that macros inside the listings environment must not get expanded – that of course includes the end tag of the environment.
So basically it looks in each line if the line contains \end{lstlisting} – but in your case, no such line exists since the \end{javacode} macro has not yet been expanded. So listings continues to search until the end of the file.
Listings defines an own command to work around this. From the documentation:
\lstnewenvironment
{⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
{⟨starting code⟩}
{⟨ending code⟩}
For example:
\lstnewenvironment{javacode}[2]
{\lstset{language=java, label=#1, caption=#2}}
{}
EDIT In response to your edited question: I tried to compile the following minimal “working” example. Actually, it’s not so much working – the latex processor just stops right in the middle and waits for a user input.
Since the listings documentation makes no mention of a special treatment of singlespace, I think you may have uncovered a bug. The best course of action is probably to get feedback from the maintainer of the listings package.
% mini.dvi
\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\doublespacing
\lstnewenvironment{javacode}
{\begin{singlespace}
\lstset{language=java}}
{\end{singlespace}}
\begin{document}
\begin{javacode}
int a = 1;
int b = 2;
\end{javacode}
\end{document}
Upon further research, I found this http://www.tug.org/pipermail/texhax/2009-June/012699.html
To workaround my solution, I need to use \singlespacing instead of the singlespace environment.
The following is now my working code:
\lstnewenvironment{javacode}[2]
{\singlespacing\lstset{language=java, label=#1, caption=#2}}
{}
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