How to show a LaTeX command in lstlisting? - latex

I code this in a .tex file.
\begin{lstlisting}
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
\end{lstlisting}
And this is what I expect:
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
Unfortunately, this is not feasible. What is the correct format?

You can use another name for the lstlisting:
\documentclass{article}
\usepackage{listings}
\lstnewenvironment{mylisting}{}{}
\begin{document}
\begin{mylisting}
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
\end{mylisting}
\end{document}

Related

lstinputlisting adds cryptic line

i am using lstinputlisting in a latex document to include some source code. When I am giving a specific line range or firstline to start with there is a cryptic line added directly before my code looking like that:
code line added by latex
My latex code:
\lstset{ % Konfiguration
xleftmargin=0.5cm,
language=C++, % C++ als Sprache vordefinieren. Optional überschreiben
basicstyle=\small,
backgroundcolor=\color{lgray},
keywordstyle=\color{black}\bfseries,
commentstyle=\color{black}\small,
showstringspaces=false,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
tabsize=4,
showtabs=false,
}
\lstinputlisting[linerange=739-745]{Marlin/Configuration.h}
Does someone know where this line comes from and how to fix it?
Minimal example:
\documentclass[12pt, ngerman]{scrreprt}
\usepackage{listings}
\begin{document}
\lstinputlisting[linerange={740-745}]{example.h}
\end{document}

include figures with multiple spaces in the filename

I have lots of files with name format case1_test 2_4.png. For some reasons, I can not remove the spaces from those filenames. Can anyone suggest how to include this kind of file using includegraphics.
I have tried several solutions including griffle package and double quotations. However none of them works.
Tried suggestions from this page
https://tex.stackexchange.com/questions/8422/how-to-include-graphics-with-spaces-in-their-path
\documentclass[10pt]{spie}
\usepackage{graphicx}
\usepackage{subfig}
\begin{document}
\begin{figure}[t]
\subfloat{
\includegraphics[height=1in]{case1_test 2_4.png}
}
\subfloat{
\includegraphics[height=1in]{case1_test 2_4.png}
}
\end{figure}
\end{document}
Using the grffile package and replacing the spaces in \includgraphics with \space seems to work fine for pdflatex and lualatex:
\documentclass[10pt]{spie}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{grffile}
\begin{document}
\begin{figure}[t]
\subfloat{
\includegraphics[height=1in]{case1_test\space\space\space\space\space2_4}
}
\end{figure}
\end{document}

Display chunk as text in listing environment (knitr)

I want to let the chunk itself be displayed as text inside the listings environment. If I enter the chunk inside listings as below it is getting evaluated.
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
<<>>=
1+1
#
\end{lstlisting}
\end{document}
Is there a way that does not include to comment <<>>= and # out with %?
Incorporating a literal code chunk is knitr FAQ 8. For this case, use
\documentclass{article}
\begin{document}
<<setup, include=FALSE>>=
render_listings()
#
\begin{lstlisting}
\Sexpr{''}<<>>=
1+1
#
\end{lstlisting}
\end{document}

How to embed LaTeX keywords inside a LaTeX document using 'listings'

I want to cite LaTeX code into my document but how do I embed the keywords "\begin{lstlisting}" and "\end{lstlisting}" correctly?
CODE BELOW DOES NOT WORK (obviously):
\lstset{language=TeX, basicstyle=\footnotesize, numbers=left, numberstyle=\tiny, frame=single}
\begin{lstlisting}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{lstlisting}
Do you have \usepackage{listings} in your preamble? If so, it should work. TeX is a supported language.
Here's a minimal example:
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
To use \texttt{lstlisting}, include this in the preamble:
\begin{lstlisting}
\usepackage{listings}
\end{lstlisting}
Hope that helped :)
\end{document}
which compiles to
EDIT
To quote commands from the listings package (actually, only for \end{lstlisting}), escape to latex to print the \ character and you're all set. In the following, I've defined # as the escape character and everything within two # symbols is typeset in LaTeX. So here, I've input the \ using LaTeX and the rest within lstlisting and the \end{...} sequence is not interpreted as a closing the environment.
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
Use escape characters to escape to \LaTeX
\lstset{escapechar=\#}
\begin{lstlisting}
\begin{lstlisting}
some code here
#\textbackslash#end{lstlisting}
\end{lstlisting}
Hope that helped :)
\end{document}
The output is
can you use a verbatim block?
\begin{verbatim}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{verbatim}
You can use
\lstnewenvironment{OtherListing}
{}
{}
to create a new envirnment that behaves just list lstlisting, and \end{lstlisting} should not be forbidden in it.

Setting up author or address string variables in LaTeX

LaTeX is a wonderful language for writing documents. With the hyperref package and pdflatex, you easily generate documents with metadata, a nice feature to get your documents referenced right on the web.
I often use templates like:
\documentclass[11pt]{article}
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={My title},%
pdfauthor={My name},%
pdfkeywords={my first keyword, my second keyword, more keywords.},%
}%
\begin{document}
\title{My title}
\author{My name}
\date{}
\maketitle
{\bf Keywords:} my first keyword, my second keyword, more keywords.%
My text is here...
\end{document}
So far, it's well. My question pops out from the example: is there a way to define string variables in the header so that they can be passed as arguments to hyperref and then to the frontmatter or to the text. Something like:
\documentclass[11pt]{article}
%-------definitions-----
\def\Author{My name}
\def\Title{My title}
\def\Keywords{my first keyword, my second keyword, more keywords.}
%--------------------------
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={\Title},%
pdfauthor={\Author},%
pdfkeywords={\Keywords},%
}%
\begin{document}
\title{\Title}
\author{\Author}
\date{}
\maketitle
{\bf Keywords:} \Keywords %
My text is here...
\end{document}
This fails for the \maketitle part and for the hyperref metadata with ! Use of \Title doesn't match ! Argument of \let has an extra }.but also for including the keywords.
The correct template should look like:
\documentclass[11pt]{article}
%-------definitions-----
\newcommand{\Author}{My name}
\newcommand{\Title}{My title}
\newcommand{\Keywords}{my first keyword, my first keyword, more keywords.}
%--------------------------
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={\Title},%
pdfauthor={\Author},%
pdfkeywords={\Keywords},%
}%
\begin{document}
\title{\Title}
\author{\Author}
\date{}
\maketitle
{\bf Keywords:} \Keywords %
My text is here...
\end{document}
Compiles fine and the metadata shows fine in the pdf reader.
Try using \newcommand{\Author}{My name} instead of \def.

Resources