是否可以让 LaTeX 忽略某些行?

是否可以让 LaTeX 忽略某些行?

我正在使用一个包来呈现一些代码,但是,虽然能够生成很好的 pdf,但编译器将代码行视为命令,并抱怨它不理解它。

有没有办法告诉编译器忽略某些代码中的命令?

用它来给代码着色并格式化:列表包:如何格式化所有数字?

代码如下:

\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}

代码本身位于

\begin{lstlisting}[style=Arduino]

答案1

#是 TeX 中的参数数字前缀字符。你必须逃避的是在关键字列表中

但是您的代码还有另一个问题。listings不适用于多个 utf8 字符例如å。这里我习惯于extendedchars=false避免这个问题,但这可能不是最好的解决办法。

\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}

在此处输入图片描述

相关内容