[Tex/LaTex] Lstinputlisting and listings styles

external fileslistings

To further extend my recent question about new listings environments, found here: Multiple listings styles.

How do I define a new lstlistings environment, via the command \lstnewenvironment, and, be able to use the command \lstinputlisting to draw the desired contents from file?

I am referring to newly created lstlistingenvironments via the command \lstnewenvironment{MYENV}. These are then accessed by

\begin{MYENV} 
BODY 
\end{MYENV} 

Anything where BODY is, gets formatted, including the command \input{mycode.whatever}, instead of pulling the contents of the file mycode.whatever and formatting the contents.

Best Answer

If the file contains the appropriate listing environment (like cplusplus or rcode), then you can just use \input{<filename>}. If it doesn't (or just contains the raw code that should be formatted), you can create a new command that does all of the setup for you, just like it would for your \lstnewenvironment creations. The following can be used in conjunction with the solution in Multiple listings styles:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{filecontents}% http://ctan.org/pkg/filecontents

\begin{filecontents*}{cplusplus.c}
// 'Hello World!' program 

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
\end{filecontents*}
\begin{filecontents*}{rcode.r}
cat('Hello, world!\n')
\end{filecontents*}
\begin{filecontents*}{pseudocode.p}
print "Hello world"
\end{filecontents*}

\makeatletter
% --------------------------------------- C++
\let\oldaddcontentsline\addcontentsline
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
    \let\contentsname\lstlistcplusplusname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\newcommand{\lstinputcplusplus}[2][]{{%
  \renewcommand{\lstlistingname}{C++ Code}%
  \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{loc}{##2}{##3}}%
  \lstinputlisting[language=C++,#1]{#2}%
}}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
    \let\contentsname\lstlistrcodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\newcommand{\lstinputrcode}[2][]{{%
  \renewcommand{\lstlistingname}{R Code}%
  \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{lor}{##2}{##3}}%
  \lstinputlisting[language=R,#1]{#2}%
}}
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
\lst@UserCommand\lstlistofpseudocode{\bgroup
    \let\contentsname\lstlistpseudocodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\newcommand{\lstinputpseudocode}[2][]{{%
  \renewcommand{\lstlistingname}{Pseudocode}%
  \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{lop}{##2}{##3}}%
  \lstinputlisting[basicstyle=\ttfamily,#1]{#2}%
}}
\makeatother
\begin{document}

\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode

\lstinputcplusplus[caption={Hello world}]{cplusplus.c}

\lstinputrcode[caption={Hello world}]{rcode.r}

\lstinputpseudocode[caption={Hello world}]{pseudocode.p}

\end{document}

The macros provided for inputting a listing of a specific type are \lstinputcplusplus[<options>]{<filename>}, \lstinputrcode[<options>]{<filename>} and \lstinputpseudocode[<options>]{<filename>}.