# How to include a highlighted source code from external tex file?

I have tried to minimise my scenario here. Basically, I would like to call each piece of highlighted code from external tex file over the beamer class.

There are two .tex files

• code.tex
• main.tex

My aim is to store codes inside code.tex and call them inside main.tex based on the title in order to tidy up everything. If I call \testCode{} on the main.tex, there are various issues. Any idea how I can handle this process ?

code.tex

\def\testCode{
\begin{lstlisting}[style=CStyle]
#include <stdio.h>
int main(void)
{
printf("Hello World!");
}
\end{lstlisting}
}

\def\testText{
Test input text
}


main.tex

\documentclass{beamer}

\mode<presentation>
{
\usecolortheme{beaver}    % or try albatross, beaver, crane, ...
\usefonttheme{serif}      % or try serif, structurebold, ...
\setbeamertemplate{caption}[numbered]
}

\usepackage{xcolor}
\usepackage{listings}

\definecolor{mGreen}{rgb}{0,0.6,0}
\definecolor{mGray}{rgb}{0.5,0.5,0.5}
\definecolor{mPurple}{rgb}{0.58,0,0.82}
\definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{CStyle}{
backgroundcolor=\color{backgroundColour},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{mGray},
stringstyle=\color{mPurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
language=C
}

\input{code.tex}

\begin{document}
\begin{frame}{title here}
\testText{}
\testCode{}
\end{frame}
\end{document}


If this can be a solution for you, I would rather have my codes in their dedicated files (e.g., code.c) and then use this command:

\lstinputlisting[style=CStyle]{code.c}


Otherwise, there is an answer here: Environment inside \newcommand

## Edit

You can also include several codes from the same file at different places of your document by using the optional arguments firstline and lastline of the \lstinputlisting commands. You can also apply different styles, so that you could mix different languages in the same source file, whatever the reason you want to do this.

I show you the result first. Then follow the codes.

Main code from the question, extended with the proposed answer:

\documentclass{beamer}

\mode<presentation>
{
\usecolortheme{beaver}    % or try albatross, beaver, crane, ...
\usefonttheme{serif}      % or try serif, structurebold, ...
\setbeamertemplate{caption}[numbered]
}

\usepackage{xcolor}
\usepackage{listings}

\definecolor{mGreen}{rgb}{0,0.6,0}
\definecolor{mGray}{rgb}{0.5,0.5,0.5}
\definecolor{mPurple}{rgb}{0.58,0,0.82}
\definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{CStyle}{
backgroundcolor=\color{backgroundColour},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{mGray},
stringstyle=\color{mPurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
language=C
}
\lstdefinestyle{LatexStyle}{
language=[LaTeX]TeX
}

\begin{document}
\begin{frame}{title here}
A C code:
\lstinputlisting[style=CStyle,firstline=2,lastline=7]{code.tex}

A \LaTeX\ code from same file:
\lstinputlisting[style=LatexStyle,firstline=10,lastline=10]{code.tex}
\end{frame}
\end{document}


Source file containing different pieces of codes to include in the document:

% First code: C
#include <stdio.h>

int main(void)
{
printf("Hello World!");
}

% Second code: LaTeX
\newcommand{\testText}{Test input text}


Enjoy!

• The point is here calling each piece of code section from the same source file based on the LaTeX definition name. The answer you gave is not what I am asking. The answer you point assumes that each piece of code is saved in different file. Although I have tried your suggestions, it could not them if each piece of code are stored in the same file whether it is .c or .tex. – ozturkib Oct 8 '19 at 8:27
• @ozturkib is it better now that I have edited my answer? – Alexandre Quenon Oct 8 '19 at 9:22
• Thanks for the update! – ozturkib Oct 8 '19 at 13:10