# Custom environment with variable indentation and auto-line breaking?

Say, I want the first line to have no indentation, but then subsequent lines to have a set amount of indentation. And I don't want to have to type "\" at the end of each line to break it. I'd also like to shorten the \begin{...} code.

So I want something like:

\BE
this is
my custom
enviro code
\EE

to output to pdf this:

this is
my custom
enviro code


I know how to create custom environments and commands, at least at a novice level by following other examples.

Basically, I am looking for a way to reproduce multiline R code exactly as it looks in the Console in RStudio with minimal typing on my part. So any suggestions as to how to do that are appreciated.

I also realize that R Markdown is probably the best option, but I'm trying to keep with traditional latex at the moment. Feel free to try and convince me otherwise as well. I also cannot get the "knit to pdf" working in RStudio, but that is a separate issue.

Here is a minimal working example:

\documentclass{article}
\usepackage{color}

\newcommand{\bR}{\ttfamily\color{blue}$>$ }
\newcommand{\eR}{\color{black}\rmfamily}

\begin{document}

Here is how I'd like to be able to type the latex code:

\bR
xbar=mean(x)
summary(x)
hist(x)
\eR

But here is what will give me the formatting that I want:

\bR
xbar=mean(x)\\
\hspace*{0.32in} summary(x) \\
\hspace*{0.32in} hist(x) \\
\eR

\end{document}


One solution would be to have a separate command for subsequent lines (probably easiest fix). Another option would be to create a custom list environment. I could probably make those happen. I'm just hoping to figure out the best and most efficient way to achieve what I want.

===

EDIT: Using the answer below, I was able to make a slight edit that I am happy with.

\newenvironment{realR}{\obeylines\parindent5mm\noindent\ttfamily\color{blue}}{}
\newcommand{\rcp}{\noindent $>$ }

\begin{realR}
\rcp x = c()
mean(x)
summary(x)
\end{realR}


Here is a quick hack that does (almost) what you want. Personally, I recommend against using short hands like \bR and \eR as they make your document harder to read, which becomes more problematic if you have coauthors. This said, using the code below, writing

\bR xbar=mean(x)
summary(x)
hist(x)
\eR


will produce:

Note that it is crucial that the xbar=mean(x) is on the same line as the \bR! (This is the "almost" in "...does (almost) what you want".)

Here is the full code:

\documentclass{article}
\newenvironment{realR}{\obeylines\parindent8mm\noindent}{}
\newcommand\bR{\begin{realR}} % shorthands for environment
\newcommand\eR{\end{realR}}

\begin{document}

\bR xbar=mean(x)
summary(x)
hist(x)
\eR

\end{document}


As you can see the idea is to change the paragraph indentation with \parindent8mm and to turn off the indentation for the first line, with \noindent (sorry, I could not bring myself to use inches...). The \obeylines command tells TeX to use the natural line breaks.

EDIT

Even though you said this clearly enough, I didn't really take in that you are typesetting R-code. For this I recommend a different approach using listings. For example, you can produce:

using the code:

\documentclass{article}
\usepackage{listings}
\begin{document}

\begin{lstlisting}[language=R]
xbar=mean(x)
summary(x)
hist(x)
\end{lstlisting}

\end{document}


With only a little more effort essentially the same code will produce:

Here is the code with the extra effort:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\lstdefinestyle{rcode}{
language=R,
backgroundcolor=\color{Gainsboro!50!White},
}
\lstnewenvironment{rcode}{\lstset{style=rcode}}{}

\begin{document}

\begin{rcode}
xbar=mean(x)
summary(x)
hist(x)
\end{rcode}

\end{document}

• Very awesome! I was able to modify your code, which I will append as an edit to my question. Is your recommendation to not shorten begin/end commands for environments? Is that a common standard practice recommendation for latex code legibility. This is just for my own lecture notes for a class I'm teaching. I just wanted a nice way to present 'verbatim' like R code that is easy for my students to copy and paste from a pdf. Jan 22, 2019 at 23:06
• It does still have a bit of an unsatisfactory behavior with respect to the first line break, but that is no big deal. The purpose of this is just to make my typesetting verbatim R code much easier. Jan 22, 2019 at 23:13
• @jdods I recommend using begin-end tags for environments, rather than short-hands like \bR...\eR because is possible to configure most editors to insert begin-end tags (so there's not much saving in typing), and the begin-end tags display better with syntax highlighting (so the code is more readable, both for you and anyone else who might read your tex file). Even though you said this clearly enough, I didn't really take in that you are typesetting R-code. For this I recommend a different approach using listings. I'll edit to explain.
– user30471
Jan 23, 2019 at 1:14
• I've never heard of the listings package, so it's good to know! Jan 23, 2019 at 7:50