[Tex/LaTex] How to set up listings for use code from Arduino

codecolorlistings

Well, I'm creating notes from a course of Arduino in LaTeX and I was reading about the listingspackage and I want to use it with the code of the exercises of the course. I know Arduino uses a language based on Processing that isn't included yet in the languages that listings can recognize.

So, my question is if there is a way to configure or set up listings for create blocks of code for Arduino and can use typographical or color schemes for distinguish comments, variables, definitions, etc.

My MWE:

\documentclass[fontsize=10pt,paper=letter,DIV=8]{scrartcl}
\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{mwe}
\usepackage{listings}    

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.47,0.47,0.33}
\definecolor{myorange}{rgb}{0.8,0.4,0}
\definecolor{mywhite}{rgb}{0.98,0.98,0.98}
\definecolor{myblue}{rgb}{0.01,0.61,0.98}

\lstset{ %
  backgroundcolor=\color{mywhite},   
  basicstyle=\footnotesize,       
  breakatwhitespace=false,         
  breaklines=true,                 
  captionpos=b,                   
  commentstyle=\color{mygray},    
  deletekeywords={...},           
  escapeinside={\%*}{*)},          
  extendedchars=true,              
  frame=shadowbox,                    
  keepspaces=true,                 
  keywordstyle=\color{myorange},       
  language=Octave,                
  morekeywords={*,...},            
  numbers=left,                    
  numbersep=5pt,                   
  numberstyle=\tiny\color{mygray}, 
  rulecolor=\color{black},         
  rulesepcolor=\color{myblue},
  showspaces=false,                
  showstringspaces=false,          
  showtabs=false,                  
  stepnumber=2,                    
  stringstyle=\color{myorange},    
  tabsize=2,                       
  title=\lstname                   
}    

\begin{document}

\section{Introduction}

\blindtext

\begin{figure}
\includegraphics[width=0.48\linewidth]{example-image}
\caption{Arduino UNO diagram.}
\end{figure}

\newpage

\section{First programs}

\minisec{Sketch 1: A flashing LED on a protoboard}

\begin{lstlisting}
 /*  
 Sketch un led intermitente en una tarjeta de pruebas. 
 Es practicamente lo mismo que un Hello World.
 En este caso cambiamos el pin del LED y usamos un LED externo, ademas del LED hay que colocar un resistor entre el LED y tierra. 
 */

void setup(){
  //inicializa el pin digital 9 como salida (output)
  pinMode(9,OUTPUT);
}

void loop(){
  digitalWrite(9,HIGH); //pone el LED en HIGH (encendido)
  delay(1000); // espera por un segundo, 1000 ms
  digitalWrite(9,LOW); // pone el LED en LOW (apagado)
  delay(1000); // espera por un segundo, 1000 ms
}
\end{lstlisting}

\end{document}

I get a nice frame with a color shadow, but I want to get also color inside the code.

Best Answer

You simply need to provide the definitions for the language:

enter image description here

You can tweak many things, but to illustrate the changes I made are:

  • comments (both the /* ... */ and the single line // are in red,
  • keywords are highligted in myorange,
  • identifiers (manually specified) are highlighted in bold blue
  • numbers (not in comments) are highlighted in green. If you want the numbers in the commens highlighted as well remove the * in literate=*.

References:

Code:

\documentclass[fontsize=10pt,paper=letter,DIV=8]{scrartcl}
\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{mwe}
\usepackage{listings}    
\usepackage{etoolbox}    

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.47,0.47,0.33}
\definecolor{myorange}{rgb}{0.8,0.4,0}
\definecolor{mywhite}{rgb}{0.98,0.98,0.98}
\definecolor{myblue}{rgb}{0.01,0.61,0.98}

\newcommand*{\FormatDigit}[1]{\ttfamily\textcolor{mygreen}{#1}}
%% https://tex.stackexchange.com/questions/32174/listings-package-how-can-i-format-all-numbers
\lstdefinestyle{FormattedNumber}{%
    literate=*{0}{{\FormatDigit{0}}}{1}%
             {1}{{\FormatDigit{1}}}{1}%
             {2}{{\FormatDigit{2}}}{1}%
             {3}{{\FormatDigit{3}}}{1}%
             {4}{{\FormatDigit{4}}}{1}%
             {5}{{\FormatDigit{5}}}{1}%
             {6}{{\FormatDigit{6}}}{1}%
             {7}{{\FormatDigit{7}}}{1}%
             {8}{{\FormatDigit{8}}}{1}%
             {9}{{\FormatDigit{9}}}{1}%
             {.0}{{\FormatDigit{.0}}}{2}% Following is to ensure that only periods
             {.1}{{\FormatDigit{.1}}}{2}% followed by a digit are changed.
             {.2}{{\FormatDigit{.2}}}{2}%
             {.3}{{\FormatDigit{.3}}}{2}%
             {.4}{{\FormatDigit{.4}}}{2}%
             {.5}{{\FormatDigit{.5}}}{2}%
             {.6}{{\FormatDigit{.6}}}{2}%
             {.7}{{\FormatDigit{.7}}}{2}%
             {.8}{{\FormatDigit{.8}}}{2}%
             {.9}{{\FormatDigit{.9}}}{2}%
             %{,}{{\FormatDigit{,}}{1}% depends if you want the "," in color
             {\ }{{ }}{1}% handle the space
             ,%
}


\lstset{%
  backgroundcolor=\color{mywhite},   
  basicstyle=\footnotesize,       
  breakatwhitespace=false,         
  breaklines=true,                 
  captionpos=b,                   
  commentstyle=\color{red},    
  deletekeywords={...},           
  escapeinside={\%*}{*)},          
  extendedchars=true,              
  frame=shadowbox,                    
  keepspaces=true,                 
  keywordstyle=\color{myorange},       
  language=Octave,                
  morekeywords={*,...},            
  numbers=left,                    
  numbersep=5pt,                   
  numberstyle=\tiny\color{mygray}, 
  rulecolor=\color{black},         
  rulesepcolor=\color{myblue},
  showspaces=false,                
  showstringspaces=false,          
  showtabs=false,                  
  stepnumber=2,                    
  stringstyle=\color{myorange},    
  tabsize=2,                       
  title=\lstname,
  emphstyle=\bfseries\color{blue},%  style for emph={} 
}    

%% language specific settings:
\lstdefinestyle{Arduino}{%
    style=FormattedNumber,
    keywords={void},%                 define keywords
    morecomment=[l]{//},%             treat // as comments
    morecomment=[s]{/*}{*/},%         define /* ... */ comments
    emph={HIGH, OUTPUT, LOW},%        keywords to emphasize
}

\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string

\begin{document}

\section{Introduction}

\blindtext

\begin{figure}
\includegraphics[width=0.48\linewidth]{example-image}
\caption{Arduino UNO diagram.}
\end{figure}

\newpage

\section{First programs}

\minisec{Sketch 1: A flashing LED on a protoboard}

\begin{lstlisting}[style=Arduino]
 /*  
 Sketch un led intermitente en una tarjeta de pruebas. 
 Es practicamente lo mismo que un Hello World.
 En este caso cambiamos el pin del LED y usamos un LED externo, ademas del LED hay que colocar un resistor entre el LED y tierra. 
 */

void setup(){
  //inicializa el pin digital 9 como salida (output)
  pinMode(9,OUTPUT);
}

void loop(){
  digitalWrite(9,HIGH); //pone el LED en HIGH (encendido)
  delay(1000); // espera por un segundo, 1000 ms
  digitalWrite(9,LOW); // pone el LED en LOW (apagado)
  delay(1000); // espera por un segundo, 1000 ms
}
\end{lstlisting}

\end{document}
Related Question