编辑于 16.05.18 17:55 添加了我想要编译的 Matlab 代码。添加
我设法通过清单在我的 Matlab 代码中编译了德语字母 Ä、Ö、Ü、ß,但现在这些字母无法在普通文本中编译。以下是一个例子:
\documentclass[a4paper,bibtotoc,oneside,10pt]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{amsmath,amssymb,amsfonts,amstext}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[inputencoding=utf8x/latin1]{listingsutf8}
%this package is needed for the matlab code compile
\lstset{
language=Matlab,
breaklines=true,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
basicstyle=\ttfamily,
columns=fullflexible,
showstringspaces=false,
commentstyle=\color{gray}\upshape,
literate=
{ß}{{\ss}}1,
}
\begin{document}
\selectlanguage{ngerman}
These dont get compiled: Ä Ö Ü ß.
They can still be written like this, but that would take very long in my thesis: \"o, \"a, \"u, \ss.
\lstinputlisting[language=Matlab, firstline=1, lastline=5]{example.m}
\end{document}
MATLAB 代码,在 Latex 文件夹中保存为“example.m”:
%test µ µ µ
[file,path]=uigetfile('*.*','Bitte Bild auswählen');
Pfad = strcat(path,file);
im=imread(Pfad);
figure
imshow(im);
现在您应该可以看到,Latex 可以编译 Matlabcode 中的 Umlaute ä、ö、ü 和 ß,但 µ 不起作用。而且文本中的 Umlaute 都无法编译。
答案1
您还可以inputencoding
在listing
环境中使用 -option。
\documentclass[a4paper,bibtotoc,oneside,10pt]{scrartcl}
\usepackage[inputencoding=utf8]{listingsutf8}
\begin{document}
\begin{lstlisting}[inputencoding={utf8},extendedchars=true]
Ä Ö Ü ü ä ö
\end{lstlisting}
\end{document}
答案2
首先,你需要使用inputenc
-package with utf8
: \usepackage[utf8]{inputenc}
。这用于在 LaTeX 中编译它们。
\documentclass[a4paper,oneside,10pt]{scrartcl}
\usepackage[utf8]{inputenc} %<--- Add this
\begin{document}
These now get compiled: Ä Ö Ü ß.
Also, as you said, these can still be written like \"o, \"a, \"u, \ss.
Although, that would take very long in your thesis.
\end{document}
接下来:您必须在literate
的部分添加大写版本\lstset
,以及µ
:
\lstset{literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}2
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{µ}{{$\mu$}}1
}
这样你lstset
就会
\lstset{
language=Matlab,
breaklines=true,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
basicstyle=\ttfamily,
columns=fullflexible,
showstringspaces=false,
commentstyle=\color{gray}\upshape,
literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}2
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{µ}{{$\mu$}}1
}