目前正在使用\usepackage{列表}因为我没遇到任何问题。但我最后两次插入时出现了一个错误:第一行插入了一个“é”。我使用的是 Overleaf,上下文如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
%Ajout de code dans latex
\usepackage{listings}
%change the caption "Listing x" to "Algorithme x"
\renewcommand\lstlistingname{}
%And to color the code
\usepackage{color} %red, green, blue, yellow, cyan, magenta, black, white
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{myblue}{RGB}{170,55,241}
\lstdefinestyle{Langage_C}{
language=C,
basicstyle=\footnotesize,
keywordstyle=\color{myblue}\ttfamily,
commentstyle=\color{mygreen}\ttfamily,
frame=single,
breaklines=true,
numbers=left,
frame=single,
extendedchars=\true,
breaklines,
rulecolor=\color{black},
}
\begin{document}
\lstinputlisting[firstline=1,lastline=64,style = Langage_C, numbers = none]{4_main.c}
\end{document}
下面是我用来插入代码的命令(源代码已包含在文件中):
以下是我以行数作为标志筛选出的代码源代码:
#include <asf.h>
#include "Constants.h"
#include "Calcul_Gabarit.h"
struct tc_module tc_instance4;
struct tc_module tc_instance6;
struct dac_module dac_instance;
int Etat = Marche; // Définition de Etat (Marche = 1)
int Vpalier = 5000; //Vient du LIN par la suite
void configure_tc4(void); // TC4 configuration procedure
void configure_tc6(void); // TC6 configuration procedure
void configure_tc_callbacks(void); // Configure callback procedure
void configure_port_pins(void); //Configure port pins
void tc_callback_LEDBlink(struct tc_module *const module_inst); // Callback procedure
void configure_dac(void);
void configure_dac_channel(void);
int Calcul_Gabarit (int Etat , int Vpalier);
typedef enum {Repos, Acceleration, VitesseConstante, Deceleration}State;
State state= Repos;
int Vconsigne=0;
int Temps=0;
int i = 0;//Used for led blinking counter
void configure_port_pins(void){
struct port_config config_port_pin;
port_get_config_defaults(&config_port_pin); // pin default configuration
config_port_pin.direction=PORT_PIN_DIR_OUTPUT; // change direction from input(default) to output
port_pin_set_config(EXT2_PWM_0_PIN,&config_port_pin); // Apply configuration to pin PB12
}
void configure_tc4(void){
struct tc_config config_tc4;
tc_get_config_defaults(&config_tc4);
config_tc4.counter_size = TC_COUNTER_SIZE_16BIT;
config_tc4.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ;
config_tc4.counter_16_bit.compare_capture_channel[0] = 7999;
config_tc4.pwm_channel[0].enabled = true; // PWM output is enabled for channel 0
config_tc4.pwm_channel[0].pin_out = EXT2_PWM_0_PIN; // W0 signal on TC4
config_tc4.pwm_channel[0].pin_mux = EXT2_PWM_0_MUX; // Mux setting for W0 signal on TC4 (PB12)
tc_init(&tc_instance4, TC4, &config_tc4);
tc_enable(&tc_instance4);
}
void configure_tc6(void){
struct tc_config config_tc6;
tc_get_config_defaults(&config_tc6);
config_tc6.counter_size = TC_COUNTER_SIZE_16BIT;
config_tc6.wave_generation = TC_WAVE_GENERATION_MATCH_PWM ;
config_tc6.counter_16_bit.compare_capture_channel[0] = PWM_CC0; //Defines the period of PWM signal
//config_tc6.counter_16_bit.compare_capture_channel[1] = Vconsigne*799/VpalierMax; //Initial value, not compulsory
config_tc6.pwm_channel[1].enabled = true; // PWM output is enabled for channel 1
config_tc6.pwm_channel[1].pin_out = EXT1_PWM_1_PIN; // W1 signal on TC6 (cf datasheet)
config_tc6.pwm_channel[1].pin_mux = EXT1_PWM_1_MUX; // Mux setting for W1 signal on TC6 (PB03)
tc_init(&tc_instance6, TC6, &config_tc6);
tc_enable(&tc_instance6);
}
void configure_dac(void){//Digital to Analog Converter
//Setup is similar to TC setup
struct dac_config config_dac;
dac_get_config_defaults(&config_dac);
dac_init(&dac_instance, DAC, &config_dac);
dac_enable(&dac_instance);
}
void configure_dac_channel(void){
struct dac_chan_config config_dac_chan;
dac_chan_get_config_defaults(&config_dac_chan);
dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan);
dac_chan_enable(&dac_instance, DAC_CHANNEL_0);
}
void configure_tc_callbacks(void){
tc_register_callback(&tc_instance4, tc_callback_LEDBlink, TC_CALLBACK_CC_CHANNEL0);
tc_enable_callback(&tc_instance4, TC_CALLBACK_CC_CHANNEL0);
}
int Calcul_Gabarit (int Etat , int Vpalier){
switch(state)
{
//State Repos
case Repos :
Vconsigne = 0;
if (Etat == Marche){
if(Temps>=TRepos){
Temps = 0;
state = Acceleration;
}else{
Temps++;
}
}
break;
//State Accel
case Acceleration :
if(Temps >= Tmontee){
Temps = 0;
Vconsigne = Vpalier;
state = VitesseConstante;
}else{
Temps++;
Vconsigne = (Vpalier*Temps)/Tmontee;
}
break;
//State Vconst
case VitesseConstante:
if (Temps >= Tpalier){
Temps = 0;
state = Deceleration;
}else{
Temps++;
}
break;
//State Decel
case Deceleration :
if(Temps >=TDescente){
Vconsigne = 0;
Temps = 0;
state = Repos;
}else{
Temps++;
Vconsigne = (Vpalier*(TDescente-Temps))/TDescente ;
}
break;
}
return Vconsigne;
}
void tc_callback_LEDBlink(struct tc_module *const module_inst){
//Blink of the LED at 1HZ
if(i ==500) {
port_pin_toggle_output_level(LED_0_PIN);
i = 0;
}else{
i++;
}
int Vconsigne2 = Calcul_Gabarit(Etat,Vpalier);
//Computation of Vconsigne every ms
dac_chan_write(&dac_instance, DAC_CHANNEL_0, Vconsigne2*Resolution_DAC/VpalierMax);
//Observation of Vconsigne (which is digital) using a DAC, on pin PA02
tc_set_compare_value(&tc_instance6, TC_COMPARE_CAPTURE_CHANNEL_1, (Vconsigne2*PWM_CC0)/VpalierMax);
//Every ms, we modify the duty cycle CC1 of TC6 using Vconsigne, scaled from 0 to 799
//Output available on PB03
}
int main (void){
system_init();
configure_tc4();
configure_tc_callbacks();
configure_dac();
configure_dac_channel();
configure_tc6();
while (1) {}
}
这是从 LaTex 代码生成的 pdf 文件,其中左上角出现了错误:
使用 MWE,我尝试插入第 9 到 64 行,看起来错误的根源就在这里:
将左侧线条的数字应用到这个“é”上,就像它是一条线一样。
谢谢你们的帮助。
答案1
你应该使用listingsutf8
:
\documentclass{article}
\usepackage[utf8]{inputenc}
%Ajout de code dans latex
\usepackage{listingsutf8}
%change the caption "Listing x" to "Algorithme x"
\renewcommand\lstlistingname{}
%And to color the code
\usepackage{color} %red, green, blue, yellow, cyan, magenta, black, white
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{myblue}{RGB}{170,55,241}
\lstdefinestyle{Langage_C}{
language=C,
basicstyle=\footnotesize,
keywordstyle=\color{myblue}\ttfamily,
commentstyle=\color{mygreen}\ttfamily,
frame=single,
breaklines=true,
numbers=left,
frame=single,
extendedchars=true,
inputencoding=utf8/latin1,
breaklines,
rulecolor=\color{black},
}
\begin{document}
\lstinputlisting[firstline=50,lastline=64,style = Langage_C, numbers = none]{4_main.c}
\lstinputlisting[firstline=1,lastline=64,style = Langage_C, numbers = none]{4_main.c}
\end{document}
另一种方法是minted
:
\documentclass{article}
\usepackage[utf8]{inputenc}
%Ajout de code dans latex
\usepackage{minted}
\setminted{fontsize=\footnotesize,breaklines,frame=single}
%And to color the code
\usepackage{color} %red, green, blue, yellow, cyan, magenta, black, white
\begin{document}
\inputminted[firstline=50,lastline=64]{c}{main.c}
\inputminted[firstline=1,lastline=64]{c}{main.c}
\end{document}