我真的很绝望。我需要 MATLAB 脚本中的重音字符。我不介意注释,但图中也有重音,这确实很重要。.tex
文件和.m
文件都以 UTF-8 编码保存(都是原生的,因为我将 MATLAB 配置为从启动时使用 UTF-8 编码)。但pdflatex
不知何故仍然认为存在无效的字节序列,事实上我证实这是错误的。
这是我的序言:
\documentclass[12pt,twoside,a4paper]{article}
\usepackage[unicode]{hyperref}
\usepackage[czech]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{float}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{attachfile}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage[document]{ragged2e}
\usepackage{siunitx}
\sisetup{
detect-mode,
detect-family,
detect-inline-family=math,
group-separator={ },
group-minimum-digits={3},
output-decimal-marker={,}
}
%\usepackage{listings}
\usepackage{listingsutf8}
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstset{
extendedchars=\true, % Use extended charset
inputencoding=utf8, % Use input encoding UTF-8
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
basicstyle=\tt\scriptsize, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=b, % sets the caption-position to bottom
commentstyle=\color{codegreen}, % comment style
deletekeywords={...}, % if you want to delete keywords from the given language
escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
firstnumber=1000, % start line enumeration with line 1000
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=MATLAB, % the language of the code
morekeywords={*,...}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{codegray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=2, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{codepurple}, % string literal style
tabsize=2, % sets default tabsize to 2 spaces
caption=\texttt{\protect\filename@parse{\lstname}\protect\filename@base.\protect\filename@ext}
% show the filename of files included with \lstinputlisting; also try caption instead of title
}
\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}
我正在尝试使用以下行导入 MATLAB 脚本文件内容:
\lstinputlisting{files/source/cast1.m}
这是我的 LaTeX 编译器对 MATLAB 脚本的解释:
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "8C.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "8D.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "A1.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "9B.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "AD.
Package inputenc: Invalid UTF-8 byte sequence.
Package inputenc: Invalid UTF-8 byte "AD.
...
但是文件编码在验证中没有显示UTF-8错误:
$ iconv -f UTF-8 files/source/cast1.m -o /dev/null; echo $?
0
这是 MATLAB 脚本的内容:
%% Projekt: POL0423 [Část I]
clc; close all; clear all; % Vyčištění pracovní plochy
%% 01: Část I.A - Zadané parametry
% Průřezy
A_0 = 15.9; % [m^2] průřez nádoby
A_2 = 2.595; % [m^2] průřez odtoku
% Koeficienty přítoku
Q_A = 15; % [m^3 * s^(-1)] přítok poč.
Q_B = 29; % [m^3 * s^(-1)] přítok mod.
Q_C = 11.5; % [m^3 * s^(-1)] přítok ust.
% Časové koficienty
t_A = 36; % [s] čas skok. změny
t_B = 81; % [s] čas snižování
t_C = 176; % [s] čas ustálení
% Fyzikální konstanty
g = 9.81; % [m * s^(-2)] tíhové zrychlení
% Diferenciální rovnice
% A_0 * dh(t)/dt = Q_1(t) - Q_2(t) = Q_1(t) - A_2 * sqrt(2gh(t))
%% 02: Část I.B - Sestavení modelu
modelB = sim('cast1_model_B'); % Simulink: simulace modelu
% Načtení dat a uložení do samostatných proměnných
% Vstupní proměnná
Scope_Q1_tB = modelB.Scope_Q1_modelB.time; % Čas
Scope_Q1_wB = modelB.Scope_Q1_modelB.signals.values; % Přítok
% Výstupní proměnná
Scope_h_tB = modelB.Scope_h_modelB.time; % Čas
Scope_h_wB = modelB.Scope_h_modelB.signals.values; % Hladina
%% 03: Část I.C - Linearizace modelu
u_0 = Q_C % Ustálený přítok
h_0 = (Q_C / A_2)^2 * 1/(2*g) % Ustálená hladina
% Matice linearizovaného modelu
A = [ (-sqrt(2) * A_2 * sqrt(g))/(2 * A_0 * sqrt(h_0)) ]
B = [ 1/A_0 ]
C = [ 1 ]
D = [ 0 ]
[lin_nom,lin_den] = ss2tf(A,B,C,D); % Převod do vnějšího popisu
G_lin = tf(lin_nom,lin_den);
modelC = sim('cast1_model_C'); % Simulink: simulace modelu
% Výstupní proměnná
Scope_h_tC = modelC.Scope_h_modelC.time; % Čas
Scope_h_wC = modelC.Scope_h_modelC.signals.values; % Hladina
% Vykreslení grafů
figure
plot(Scope_Q1_tB, Scope_Q1_wB)
grid on
range=ylim();
ylim([0 range(2)])
title('Přítok Q_1(t)')
xlabel('$t$ [s]','interpreter','latex')
ylabel('$Q_1(t)$ [$\mathrm{m}^3 \cdot \mathrm{s}^{-1}$]',...
'interpreter','latex')
figure
plot(Scope_h_tB, Scope_h_wB, Scope_h_tC, Scope_h_wC)
grid on
title('Hladina h(t)')
legend('Nelineární model','Linearizovaný model')
xlabel('$t$ [s]','interpreter','latex')
ylabel('$h(t)$ [m]','interpreter','latex')
%% 04: Část I.D - Zvýšení přítoku a porovnání s linearizovaným modelem
modelD = sim('cast1_model_D'); % Simulink: simulace modelu
% Vstupní proměnná
Scope_Q1_tD = modelD.Scope_Q1_modelD.time; % Čas
Scope_Q1_wD = modelD.Scope_Q1_modelD.signals.values; % Přítok
% Výstupní proměnná
Scope_h_tD_nelin = modelD.Scope_h_modelD_nelin.time; % Čas
Scope_h_wD_nelin = modelD.Scope_h_modelD_nelin.signals.values;
% Hladina
Scope_h_tD_lin = modelD.Scope_h_modelD_lin.time; % Čas
Scope_h_wD_lin = modelD.Scope_h_modelD_lin.signals.values;
% Hladina
% Vykreslení grafů
figure
plot(Scope_Q1_tD, Scope_Q1_wD)
grid on
range=ylim();
ylim([0 range(2)])
title('Přítok Q_1(t)')
xlabel('$t$ [s]','interpreter','latex')
ylabel('$Q_1(t)$ [$\mathrm{m}^3 \cdot \mathrm{s}^{-1}$]',...
'interpreter','latex')
figure
plot(Scope_h_tD_nelin, Scope_h_wD_nelin, Scope_h_tD_lin, Scope_h_wD_lin)
grid on
title('Hladina h(t)')
legend('Nelineární model','Linearizovaný model')
xlabel('$t$ [s]','interpreter','latex')
ylabel('$h(t)$ [m]','interpreter','latex')
%% 05: Uložení parametrů a výpočtů
save('save_param',...
't_A','t_B','t_C',...
'Q_A','Q_B','Q_C',...
'A_0','A_2',...
'g',...
'A','B','C','D',...
'u_0','h_0')
有人知道为什么会发生这种情况吗?我是不是忽略了什么?
答案1
好的,这就是我最终做的事情,终于解决了我的问题。我将extendedchars=true
选项重新添加到列表设置中,然后我意识到我忘记包含一些字符了。一旦我将它们包含进去,问题就消失了。
这是我listingsutf8
在序言中的设置:
\documentclass[12pt,twoside,a4paper]{article}
\usepackage[unicode]{hyperref}
\usepackage[czech]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{float}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{attachfile}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage[document]{ragged2e}
% ...
\usepackage{listingsutf8}
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstset{
%inputencoding=utf8, % Use input encoding UTF-8
extendedchars=true, % Use extended character set
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
basicstyle=\tt\scriptsize, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=t, % sets the caption-position to bottom
commentstyle=\color{codegreen}, % comment style
escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
firstnumber=1, % start line enumeration with line 1000
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=MATLAB, % the language of the code
morekeywords={*,...}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{codegray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{codepurple}, % string literal style
tabsize=4, % sets default tabsize to 2 spaces
caption=\texttt{\protect\filename@parse{\lstname}\protect\filename@base.\protect\filename@ext},
% show the filename of files included with \lstinputlisting; also try caption instead of title
literate= % Replace Czech characters with composite letters counterpart
% áÁ čČ ďĎ éÉ ěĚ íÍ ňŇ óÓ řŘ šŠ ťŤ úÚ ůŮ
{á}{{\'{a}}}1 {Á}{{\'{A}}}1 {č}{{\v{c}}}1 {Č}{{\v{C}}}1
{ď}{{\v{d}}}1 {Ď}{{\v{D}}}1 {é}{{\'{e}}}1 {É}{{\'{E}}}1
{ě}{{\v{e}}}1 {Ě}{{\v{E}}}1 {í}{{\'{i}}}1 {Í}{{\'{I}}}1
{ň}{{\v{n}}}1 {Ň}{{\v{N}}}1 {ó}{{\'{o}}}1 {Ó}{{\'{O}}}1
{ř}{{\v{r}}}1 {Ř}{{\v{R}}}1 {š}{{\'{s}}}1 {Š}{{\'{S}}}1
{ť}{{\v{t}}}1 {Ť}{{\v{T}}}1 {ú}{{\'{u}}}1 {Ú}{{\'{U}}}1
{ů}{{\r{u}}}1 {Ů}{{\r{U}}}1 {ž}{{\v{z}}}1 {Ž}{{\v{Z}}}1
{ý}{{\'{y}}}1 {Ý}{{\'{Y}}}1
}
% ...
谢谢你,也为我愚蠢的行为道歉。我错过了编译器告诉我的内容,或者说我不太明白。现在我知道我需要用复合字符替换 Unicode 字符。不确定这对阿拉伯语或中文会怎样,但这是一个完全不同的问题,我想我永远不会遇到这些问题数千个字符语言。这是一个相当繁琐的解决方法,但可以满足我的需求。不需要很漂亮。