Display chunk as text in listing environment (knitr) - latex

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}

Related

How to show a LaTeX command in lstlisting?

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}

Add line numbers to Rmarkdown text and have it restart on each page

I am knitting my markdown file and I have included the following in my header, which produces line numbers in my text. How can I make the numbering reset at the beginning of each new page?
header-includes:
\usepackage{lineno}
\linenumbers
The lineno package has an option to restart the line number automatically for each page:
\usepackage[pagewise]{lineno}
Minimal working example:
\documentclass{article}
\usepackage[pagewise]{lineno}
\linenumbers
\usepackage{lipsum}
\begin{document}
\lipsum
\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.

How can I get page numbers to link to the table of contents in latex?

I've seen a pdf LaTeX document where the page numbers at the bottom of the page are hyperref links, and clicking them causes you to jump to the contents table. I don't have the tex file and couldn't work out how it's done from the hyperref package. Can anyone help?
You could set an anchor at the toc and redefine \thepage to link to it. Here's an example:
\documentclass{report}
\usepackage[colorlinks]{hyperref}
\renewcommand*{\contentsname}{\hyperlink{contents}{Contents}}
\renewcommand*{\thepage}{\hyperref[contents]{\arabic{page}}}
\begin{document}
\tableofcontents
\chapter{One}
Text
\end{document}
If you use babel and wish to redefine \contentsname, use the \addto command of babel or redefine \contentsname after \begin{document}.
Have you tried defining the page numbering using this?
\pagestyle{myheadings}
\markright{ ... }
where \markright specifies the page number with a link to the content page.
Here is how I did it
% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8, english]{article}
\usepackage{lipsum} %% produce dummy texts
\usepackage{hyperref}
\usepackage[pagestyles]{titlesec}
\newpagestyle{article}{
\setfoot
%% even pages
[]
[\footnotesize \hyperlink{toc}{\thepage}]
[]
%% odd pages
{}
{\footnotesize \hyperlink{toc}{\thepage}}
{}
}
\begin{document}
\title{example}
\date{}
\author{author}
\maketitle
\pagenumbering{roman}
\setcounter{tocdepth}{2}
\addtocontents{toc}{\protect\hypertarget{toc}{}}
\tableofcontents
\newpage
\pagenumbering{arabic}
\pagestyle{article}
\section{A}
\lipsum[1]
\subsection{a}
\lipsum[2]
\subsection{b}
\lipsum[3]
\subsection{c}
\lipsum[4]
\section{B}
\lipsum[5]
\subsection{d}
\lipsum[6]
\subsection{e}
\lipsum[7]
\section{C}
\lipsum[8]
\subsection{f}
\lipsum[9]
\subsection{g}
\lipsum[10]
\subsection{h}
\lipsum[11]
\end{document}
you can of course customize the link text back to table of contents however you like in the preamble, please read documentation of titlesec for more details.

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