我已经创建了以下文件
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\tcbset{
codestyle/.style={width=0.9\textwidth,boxrule=0pt,colback=blue,arc=0pt,auto outer arc,left=0pt,right=0pt,boxsep=5pt,fontupper=\ttfamily, colupper=yellow}
}
\newtcblisting{code}{listing only, codestyle}
% Document
\begin{document}
Пример кода:
\begin{code}
print("English comment"); // English comment
print("Russian comment"); // Русский комментарий
\end{code}
\end{document}
编码是无 BOM 的 UTF-8,但我可以使用我的编辑器(Jetbrains Idea)添加它。
不幸的是,当我尝试编译它时,它code
只会在环境之外绘制俄文字母。
为何以及如何修复?
我已经在Windows下安装了MiKTeX:
>where pdflatex
C:\Users\Dmitry\AppData\Local\Programs\MiKTeX\miktex\bin\x64\pdflatex.exe
>pdflatex -version
MiKTeX-pdfTeX 4.10 (MiKTeX 21.12)
© 1982 D. E. Knuth, © 1996-2021 Hàn Thế Thành
TeX is a trademark of the American Mathematical Society.
using bzip2 version 1.0.8, 13-Jul-2019
compiled with curl version 7.72.0; using libcurl/7.72.0 Schannel
compiled with expat version 2.2.10; using expat_2.2.10
compiled with jpeg version 9.4
compiled with liblzma version 50020052; using 50020052
compiled with libpng version 1.6.37; using 1.6.37
compiled with libressl version LibreSSL 3.1.4; using LibreSSL 3.1.4
compiled with MiKTeX Application Framework version 4.2.1; using 4.2.1
compiled with MiKTeX Core version 4.10; using 4.10
compiled with MiKTeX Archive Extractor version 4.0; using 4.0
compiled with MiKTeX Package Manager version 4.5; using 4.5
compiled with uriparser version 0.9.4
compiled with xpdf version 4.02
compiled with zlib version 1.2.11; using 1.2.11
答案1
包默认tcolorbox
使用listings
,它无法正确处理多字节编码。允许tcolorbox
您指定将传递给列表引擎进行处理的单字节编码:listing utf8=cp1251
。副作用 - 您还需要使用 babel 包指定语言。
\documentclass[11pt]{article}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{amsmath}
\tcbset{
codestyle/.style={width=0.9\textwidth,boxrule=0pt,colback=blue,arc=0pt,auto outer arc,left=0pt,right=0pt,boxsep=5pt,fontupper=\ttfamily, colupper=yellow},
listing utf8=cp1251
}
\newtcblisting{code}{listing only, codestyle}
% Document
\begin{document}
Пример кода:
\begin{code}
print("English comment"); // English comment
print("Russian comment"); // Русский комментарий
\end{code}
\end{document}
答案2
我已经为您制作了完全定制的文件,使用\lstlisting
它很容易,然后您的列表使用tcolorbox
(自己的想法)
\documentclass{article}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}
\lstset{ keepspaces=true,
backgroundcolor=\color{blue},
showstringspaces=false,% for spaces between rus. words
language=C,
extendedchars=\true,%for russian
framexrightmargin=0pt,
framexleftmargin=0pt,
framextopmargin=15pt,
framexbottommargin=15pt,
frame=tb, framerule=0pt,
basicstyle=\color{yellow}\ttfamily\small}
\begin{lstlisting}
print("English comment"); // English comment
print("Russian comment"); // Русский комментарий
\end{lstlisting}
\end{document}