Custom caption prefix - latex

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}

Related

Unsuccessful changing 'Chapter' to 'Annex'

I am trying to change the title 'Chapter' in appendices into 'Annex' but am unable to do so. I looked up and found the solution as
\renewcommand\appendixname{Annex}
but this does not solve my problem and the title still shows as 'Chapter'.
My guess is it might be because I have changed the titleformat of the chapters (or it might not be because of it....)
If someone could help me with the issue. My code in the preamble looks like:
\usepackage[titletoc]{appendix}
\titleformat{\chapter}[display]
{\normalfont\LARGE\bfseries\center}{Chapter \thechapter}{0.3em}{\LARGE}
\titlespacing*{\chapter}{0pt}{-15pt}{15pt}
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
\renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}
\renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}
\titleformat{\section}
{\normalfont\fontsize{14}{10}\bfseries}{\thesection}{1em}{\fontsize{14}{10}}
\renewcommand{\appendixname}{Annex}
\begin{document}
All contents that matters.....
\begin{appendices}
\chapter{Some Annex}
\input{Chapters/Appendix}
\end{appendices}
\end{document}
The problem is that you hardcoded the word Chapter here:
\titleformat{\chapter}[display]
{\normalfont\LARGE\bfseries\center}{Chapter \thechapter}{0.3em}{\LARGE}
To use the right word, regardless of whether you're in the main content or in the appendices, use \chaptertitlename as documented in the titlesec package documentation (page 4):
\chaptertitlename
It defaults to \chaptername except in appendices where it is \appendixname. Use it instead of \chaptername when defining a chapter.
Remember to add {} to make the trailing space significant:
\titleformat{\chapter}[display]
{\normalfont\LARGE\bfseries\center}{\chaptertitlename{} \thechapter}{0.3em}{\LARGE}

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 lstlisting underlined

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}

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