插入 C++ 代码后出现编码错误

插入 C++ 代码后出现编码错误

我正在用 C++ 代码编写一个小文档,插入一些新代码后,运行时开始收到以下错误:pdflatex my_doc_name.tex

! Package inputenc Error: Invalid UTF-8 byte sequence.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
l.26 cout << "3. Sucesión
                           Numerica." << endl;

我尝试使用\UseRawInputEncoding,但某些字符(例如“ú”)无法在我的文档中呈现。

举一个最小的例子,这是我的文档的一部分,我也使用 nvim 作为我的编辑器。

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{listings}

\title{Foo}
\author{Bar}
\date{Agosto 2021}

\begin{document}

\begin{titlepage}
\maketitle
\end{titlepage}

\section{Menú}


\begin{lstlisting}[language=c++]

cout << "Programas Disponibles" << endl;
cout << "1. Calculador de Promedio" << endl;
cout << "2. Contador de Digitos." << endl;
cout << "3. Sucesión Numerica." << endl;

int programa;
cout << "Ingrese una opción: ";
cin >> programa;

switch(programa) {
    case 1:
        // Programa Calculador de promedio
        break;
    case 2:
        // Programa Contador de Digitos
        break;
    case 3:
        // Programa Susesión Numerica
        break;
    default:
        cout << "Esa no es una opción valida";
}
\end{lstlisting}

\end{document}

我刚开始学习 Latex...希望这不是重复的 D:

答案1

如果在列表中使用非 ASCII 字符,则应使用列表的文学选项来声明替换:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{listings}

\title{Foo}
\author{Bar}
\date{Agosto 2021}

\lstset{
literate=
 {á}{{\'a}}1 
 {ã}{{\~a}}1 
 {é}{{\'e}}1 
 {ó}{{\'o}}1
 %extend the list as needed
 }
 
\begin{document}

\begin{titlepage}
\maketitle
\end{titlepage}

\section{Menú}

Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts.

\begin{lstlisting}[language=c++]

cout << "Programas Disponibles" << endl;
cout << "1. Calculador de Promedio" << endl;
cout << "2. Contador de Digitos." << endl;
cout << "3. Sucesión Numerica." << endl;

int programa;
cout << "Ingrese una opción: ";
cin >> programa;

switch(programa) {
    case 1:
        // Programa Calculador de promedio
        break;
    case 2:
        // Programa Contador de Digitos
        break;
    case 3:
        // Programa Susesión Numerica
        break;
    default:
        cout << "Esa no es una opción valida";
}
\end{lstlisting}

\end{document}

答案2

要么添加\usepackage[utf8]{inputenc},这会留下一个警告,要么不要使用两者,不会留下任何警告,正如所示。

要了解更多背景信息,请参阅 Latex-Wikibooks,您也可以下载:https://en.wikibooks.org/wiki/LaTeX

\documentclass[12pt]{article}
%\usepackage[utf8]{inputenc}%<<==
%\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{listings}

\title{Foo}
\author{Bar}
\date{Agosto 2021}

\begin{document}

\begin{titlepage}
\maketitle
\end{titlepage}

\section{Menú}

Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. Some text, to show the difference of fonts. 

\begin{lstlisting}[language=c++]

cout << "Programas Disponibles" << endl;
cout << "1. Calculador de Promedio" << endl;
cout << "2. Contador de Digitos." << endl;
cout << "3. Sucesión Numerica." << endl;

int programa;
cout << "Ingrese una opción: ";
cin >> programa;

switch(programa) {
    case 1:
        // Programa Calculador de promedio
        break;
    case 2:
        // Programa Contador de Digitos
        break;
    case 3:
        // Programa Susesión Numerica
        break;
    default:
        cout << "Esa no es una opción valida";
}
\end{lstlisting}

\end{document}

结果

菜单部分作为示例

相关内容