4

I am using a package to render some code, however, while being able to generate the pdf fine, the compiler sees the lines of codes as commands, and complains that it doesn't understand it.

Is there a way to tell the compiler to ignore the commands in some of the code?

Used this to color and format the code: Listings package: How can I format all numbers?

And the code:

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

\renewcommand{\thepage}{\roman{page}}

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygreen2}{rgb}{0.37,0.43,0.01}
\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}
\definecolor{myblueL}{rgb}{0,0.59,0.61}    % NY! Lysere
\definecolor{myblueM}{rgb}{0,0.36,0.37}       % NY! Mørkere

%\newcommand*{\FormatDigit}[1]{\ttfamily\textcolor{mygreen}{#1}}
%% http://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{mygray},    
  deletekeywords={...},           
  escapeinside={\%*}{*)},          
  extendedchars=true,              
  frame=shadowbox,                    
  keepspaces=true,                 
  keywordstyle=\color{mygreen2},
  keywordstyle=[2]\color{myorange},
  language=C++,                
  morekeywords={*,...},  
  numbers=left,                    
  numbersep=5pt,                   
  numberstyle=\tiny\color{mygray}, 
  rulecolor=\color{black},         
  rulesepcolor=\color{myblue},
  showspaces=false,                
  showstringspaces=false,          
  showtabs=false,                  
  stepnumber=1,                    
  stringstyle=\color{myblueM},    
  tabsize=2,                       
  title=\lstname,
  emphstyle=\color{myblueL},%  style for emph={}
  % emphstyle=\bfseries\color{blue},%  style for emph={} 
}    

%% language specific settings:
\lstdefinestyle{Arduino}{%
    style=FormattedNumber,
    keywords={return, , #define, break, return, if, for, else, while, #endif,#ifndef, loop, setup },
    keywords=[2]{Serial, begin, delay, pinMode, digitalWrite, millis, Serial1, available, readString, print, analogRead, length, toCharArray, strstr, remove, toInt},
    morecomment=[l]{//},%             treat // as comments
    morecomment=[s]{/*}{*/},%         define /* ... */ comments
    emph={int, uint8_t, String, long, float, HIGH, LOW, OUTPUT, INPUT, char, bool, void, private, public, class},%        keywords to emphasize
}

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

\begin{document}
\section{Hardware Read} \label{appendix:HWR}

\begin{lstlisting}[style=Arduino]
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

void setup() {
  Serial.begin(115200);
  ADCSRA &= ~PS_128;  // remove bits set by Arduino library
}

void loop() {
  int x;
  unsigned long t, t2;
  Serial.print("setting prescaler to 128 ");

  sbi(ADCSRA, ADPS2) ;
  sbi(ADCSRA, ADPS1) ;
  sbi(ADCSRA, ADPS0) ;

  delay(1000);
  t = millis();
  for (int i = 0; i < 1000; i++) {
    x = analogRead(A0);

  }

  t2 = millis();
  Serial.print("10000 konverteringer på ");
  Serial.print(t2 - t);  Serial.print("msec | ");
  Serial.print("setting prescaler to 16");
   // set prescale to 16
  sbi(ADCSRA, ADPS2) ;
  cbi(ADCSRA, ADPS1) ;
  cbi(ADCSRA, ADPS0) ;
  t = millis();
  for (int i = 0; i < 1000; i++) {
    x = analogRead(A0);

  }

  t2 = millis();
  Serial.print(" 10000 konverteringer på ");
  Serial.println(t2-t);
}
\end{lstlisting}

\end{document}

The code itself stars at

\begin{lstlisting}[style=Arduino]
Fink
  • 43
  • 3
    Welcome to TeX.SX! You should always show us a minimal but working example (MWE) that illustrates your problem. This would help us to reproduce the problem, to understand it and to test our own suggestions. So it would help us to help you. Without such an MWE questions are often somehow unclear. – Schweinebacke May 12 '17 at 10:38
  • 1
    If you already use listings, the MWE is not only recommended but needed, because generally listings does not interprete any command in the code of a lstlisting environment or \lstinline code or the file of a \lstinputlisting. – Schweinebacke May 12 '17 at 10:43
  • I generally get errors such as "Illegal parameter number in definition of \lststy@arduino$."...

    Here is the code: https://pastebin.com/tGUtAAqm

    – Fink May 12 '17 at 10:47
  • Yeah, new to stackexchange :). Added it to the main question. – Fink May 12 '17 at 10:50

1 Answers1

5

# is the argument number prefix character in TeX. You have to escape is in the list of keywords.

But there is another problem with your code. listings does not work for several utf8 characters, e.g., å. Here I've used extendedchars=false to avoid the problem, but this may not be the best solution.

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

\renewcommand{\thepage}{\roman{page}}

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygreen2}{rgb}{0.37,0.43,0.01}
\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}
\definecolor{myblueL}{rgb}{0,0.59,0.61}    % NY! Lysere
\definecolor{myblueM}{rgb}{0,0.36,0.37}       % NY! Mørkere

\newcommand*{\FormatDigit}[1]{\ttfamily\textcolor{mygreen}{#1}}
%% http://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{mygray},    
  deletekeywords={...},           
  escapeinside={\%*}{*)},          
  extendedchars=true,              
  frame=shadowbox,                    
  keepspaces=true,                 
  keywordstyle=\color{mygreen2},
  keywordstyle=[2]\color{myorange},
  language=C++,                
  morekeywords={*,...},  
  numbers=left,                    
  numbersep=5pt,                   
  numberstyle=\tiny\color{mygray}, 
  rulecolor=\color{black},         
  rulesepcolor=\color{myblue},
  showspaces=false,                
  showstringspaces=false,          
  showtabs=false,                  
  stepnumber=1,                    
  stringstyle=\color{myblueM},    
  tabsize=2,                       
  title=\lstname,
  emphstyle=\color{myblueL},%  style for emph={}
  % emphstyle=\bfseries\color{blue},%  style for emph={}
  extendedchars=false,
}    

%% language specific settings:
\lstdefinestyle{Arduino}{%
    style=FormattedNumber,
    keywords={return, , \#define, break, return, if, for, else, while, \#endif,\#ifndef, loop, setup },
    keywords=[2]{Serial, begin, delay, pinMode, digitalWrite, millis, Serial1, available, readString, print, analogRead, length, toCharArray, strstr, remove, toInt},
    morecomment=[l]{//},%             treat // as comments
    morecomment=[s]{/*}{*/},%         define /* ... */ comments
    emph={int, uint8_t, String, long, float, HIGH, LOW, OUTPUT, INPUT, char, bool, void, private, public, class},%        keywords to emphasize
}

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

\begin{document}
\section{Hardware Read} \label{appendix:HWR}

\begin{lstlisting}[style=Arduino]
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

void setup() {
  Serial.begin(115200);
  ADCSRA &= ~PS_128;  // remove bits set by Arduino library
}

void loop() {
  int x;
  unsigned long t, t2;
  Serial.print("setting prescaler to 128 ");

  sbi(ADCSRA, ADPS2) ;
  sbi(ADCSRA, ADPS1) ;
  sbi(ADCSRA, ADPS0) ;

  delay(1000);
  t = millis();
  for (int i = 0; i < 1000; i++) {
    x = analogRead(A0);

  }

  t2 = millis();
  Serial.print("10000 konverteringer på ");
  Serial.print(t2 - t);  Serial.print("msec | ");
  Serial.print("setting prescaler to 16");
   // set prescale to 16
  sbi(ADCSRA, ADPS2) ;
  cbi(ADCSRA, ADPS1) ;
  cbi(ADCSRA, ADPS0) ;
  t = millis();
  for (int i = 0; i < 1000; i++) {
    x = analogRead(A0);

  }

  t2 = millis();
  Serial.print(" 10000 konverteringer på ");
  Serial.println(t2-t);
}
\end{lstlisting}

\end{document}

enter image description here

Schweinebacke
  • 26,336
  • Right, forgot to remove that last letter, wasn't supposed to be there. But, helped dramatically with the errors. Thanks! – Fink May 12 '17 at 11:10