我正在学习 LaTex,并寻找将我的 c# 代码插入 LaTex 文档的方法,我从 github gists 中找到了这种样式,格式非常好,但从文件抓取的代码部分之外的字体颜色仍然受到影响,我不知道为什么会发生这种情况。我的 .tex 的序言如下所示:
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{float}
\graphicspath{{images/}}
\definecolor{base0}{RGB}{131,148,150}
\definecolor{base01}{RGB}{88,110,117}
\definecolor{base2}{RGB}{238,232,213}
\definecolor{sgreen}{RGB}{133,153,0}
\definecolor{sblue}{RGB}{38,138,210}
\definecolor{scyan}{RGB}{42,161,151}
\definecolor{smagenta}{RGB}{211,54,130}
\newcommand\digitstyle{\color{smagenta}}
\newcommand\symbolstyle{\color{base01}}
\makeatletter
\newcommand{\ProcessDigit}[1]
{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\digitstyle #1}%
\else
#1%
\fi
}
\makeatother
\lstdefinestyle{solarizedcsharp} {
language=[Sharp]C,
frame=lr,
linewidth=160mm,
breaklines=true,
tabsize=2,
numbers=left,
numbersep=5pt,
firstnumber=auto,
numberstyle=\tiny\ttfamily\color{base0},
rulecolor=\color{base2},
basicstyle=\footnotesize\ttfamily,
commentstyle=\color{base01},
morecomment=[s][\color{base01}]{/*+}{*/},
morecomment=[s][\color{base01}]{/*-}{*/},
morekeywords={ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string, var},
keywordstyle=\bfseries\color{sgreen},
showstringspaces=false,
stringstyle=\color{scyan},
identifierstyle=\color{sblue},
extendedchars=true,
literate=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E}}1 {Ĩ}{{\~I}}1 {Õ}{{\~O}}1 {Ũ}{{\~U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\euro}}1 {£}{{\pounds}}1 {«}{{\guillemotleft}}1
{»}{{\guillemotright}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1 {¡}{{!`}}1,
}
\lstset{escapechar=@,style=solarizedcsharp}
pdf 中发生的情况如下:
变成这样:
我希望第二个不受 c# 代码的 solarized 主题样式的影响,但我甚至不知道它为什么会受到影响!以下是我从 c# 文件插入代码的方法:
\subsection{Código}
\lstinputlisting{code/Ejercicio1.cs}
\subsection{Capturas de Pantalla}
编辑:这是显示样式错误的测试文档。
\documentclass{article}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{base0}{RGB}{131,148,150}
\definecolor{base01}{RGB}{88,110,117}
\definecolor{base2}{RGB}{238,232,213}
\definecolor{sgreen}{RGB}{133,153,0}
\definecolor{sblue}{RGB}{38,138,210}
\definecolor{scyan}{RGB}{42,161,151}
\definecolor{smagenta}{RGB}{211,54,130}
\newcommand\digitstyle{\color{smagenta}}
\newcommand\symbolstyle{\color{base01}}
\makeatletter
\newcommand{\ProcessDigit}[1]
{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\digitstyle #1}%
\else
#1%
\fi
}
\makeatother
\lstdefinestyle{solarizedcsharp} {
language=[Sharp]C,
frame=lr,
linewidth=160mm,
breaklines=true,
tabsize=2,
numbers=left,
numbersep=5pt,
firstnumber=auto,
numberstyle=\tiny\ttfamily\color{base0},
rulecolor=\color{base2},
basicstyle=\footnotesize\ttfamily,
commentstyle=\color{base01},
morecomment=[s][\color{base01}]{/*+}{*/},
morecomment=[s][\color{base01}]{/*-}{*/},
morekeywords={ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string, var},
keywordstyle=\bfseries\color{sgreen},
showstringspaces=false,
stringstyle=\color{scyan},
identifierstyle=\color{sblue},
extendedchars=true,
literate=
{0}{{{\ProcessDigit{0}}}}1
{1}{{{\ProcessDigit{1}}}}1
{2}{{{\ProcessDigit{2}}}}1
{3}{{{\ProcessDigit{3}}}}1
{4}{{{\ProcessDigit{4}}}}1
{5}{{{\ProcessDigit{5}}}}1
{6}{{{\ProcessDigit{6}}}}1
{7}{{{\ProcessDigit{7}}}}1
{8}{{{\ProcessDigit{8}}}}1
{9}{{{\ProcessDigit{9}}}}1
{\}}{{\symbolstyle{\}}}}1
{\{}{{\symbolstyle{\{}}}1
{(}{{\symbolstyle{(}}}1
{)}{{\symbolstyle{)}}}1
{=}{{\symbolstyle{$=$}}}1
{;}{{\symbolstyle{$;$}}}1
{>}{{\symbolstyle{$>$}}}1
{<}{{\symbolstyle{$<$}}}1
{\%}{{\symbolstyle{$\%$}}}1
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{ã}{{\~a}}1 {ẽ}{{\~e}}1 {ĩ}{{\~i}}1 {õ}{{\~o}}1 {ũ}{{\~u}}1
{Ã}{{\~A}}1 {Ẽ}{{\~E}}1 {Ĩ}{{\~I}}1 {Õ}{{\~O}}1 {Ũ}{{\~U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\euro}}1 {£}{{\pounds}}1 {«}{{\guillemotleft}}1
{»}{{\guillemotright}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1 {¡}{{!`}}1,
}
\lstset{escapechar=@,style=solarizedcsharp}
\begin{document}
\section{This appears normal}
\blindtext
\section{Code Here}
%I use the \lstinputlisting{filename} command but the problem appears with this one as well, only lstinline doesn't have this styling problem.
\begin{lstlisting}
var a = "Dummy Code!"
\end{lstlisting}
\section{This has different styling!}
\blindtext
\end{document}
答案1
您正在将其定义\symbolstyle
为切换命令。这意味着\smybolstyle
当前组中的所有文本都会获得新的颜色,例如
some text {more text \color{red} new colour} old colour
但是,您使用的\smybolstyle
不是开关,而是带有文本参数的宏(hello \symbolsstle{new colour} old colour
例如
{\}}{{\symbolstyle{\}}}}1
你使用\symbolstyle{\}}
。如果你尝试一下,你就会明白这是怎么回事
some text more text \color{red}{new colour} NOT old colour
所以你的定义\symbolstyle
应该有所不同:你希望它表现得像一个接受参数并且仅适用于该参数的文本命令。
你可以说
\newcommand\symbolstyle[1]{{\color{blue}#1}}
保持颜色变化与由附加花括号开始和结束的组一起本地化,或者使用\textcolor
隐式参数
\newcommand\symbolstyle{\textcolor{blue}}
或明确的论点
\newcommand\symbolstyle[1]{\textcolor{blue}{#1}}
这是有效的,因为\textcolor
它被定义为仅为其文本参数着色的文本命令。
您使用\digitstyle
(其定义类似于\symbolstyle
)是可以的,因为在
{\digitstyle #1}%
您将其用作开关而不是文本宏。
您的 MWE 具有固定的定义\symbolstyle
\documentclass{article}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{base0}{RGB}{131,148,150}
\definecolor{base01}{RGB}{88,110,117}
\definecolor{base2}{RGB}{238,232,213}
\definecolor{sgreen}{RGB}{133,153,0}
\definecolor{sblue}{RGB}{38,138,210}
\definecolor{scyan}{RGB}{42,161,151}
\definecolor{smagenta}{RGB}{211,54,130}
\newcommand\digitstyle{\color{smagenta}}
\newcommand\symbolstyle{\textcolor{base01}}
\makeatletter
\newcommand{\ProcessDigit}[1]
{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\digitstyle #1}%
\else
#1%
\fi
}
\makeatother
\lstdefinestyle{solarizedcsharp} {
language=[Sharp]C,
frame=lr,
linewidth=160mm,
breaklines=true,
tabsize=2,
numbers=left,
numbersep=5pt,
firstnumber=auto,
numberstyle=\tiny\ttfamily\color{base0},
rulecolor=\color{base2},
basicstyle=\footnotesize\ttfamily,
commentstyle=\color{base01},
morecomment=[s][\color{base01}]{/*+}{*/},
morecomment=[s][\color{base01}]{/*-}{*/},
morekeywords={ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string, var},
keywordstyle=\bfseries\color{sgreen},
showstringspaces=false,
stringstyle=\color{scyan},
identifierstyle=\color{sblue},
extendedchars=true,
literate=
{0}{{{\ProcessDigit{0}}}}1
{1}{{{\ProcessDigit{1}}}}1
{2}{{{\ProcessDigit{2}}}}1
{3}{{{\ProcessDigit{3}}}}1
{4}{{{\ProcessDigit{4}}}}1
{5}{{{\ProcessDigit{5}}}}1
{6}{{{\ProcessDigit{6}}}}1
{7}{{{\ProcessDigit{7}}}}1
{8}{{{\ProcessDigit{8}}}}1
{9}{{{\ProcessDigit{9}}}}1
{\}}{{\symbolstyle{\}}}}1
{\{}{{\symbolstyle{\{}}}1
{(}{{\symbolstyle{(}}}1
{)}{{\symbolstyle{)}}}1
{=}{{\symbolstyle{$=$}}}1
{;}{{\symbolstyle{$;$}}}1
{>}{{\symbolstyle{$>$}}}1
{<}{{\symbolstyle{$<$}}}1
{\%}{{\symbolstyle{$\%$}}}1
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{ã}{{\~a}}1 {ẽ}{{\~e}}1 {ĩ}{{\~i}}1 {õ}{{\~o}}1 {ũ}{{\~u}}1
{Ã}{{\~A}}1 {Ẽ}{{\~E}}1 {Ĩ}{{\~I}}1 {Õ}{{\~O}}1 {Ũ}{{\~U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\euro}}1 {£}{{\pounds}}1 {«}{{\guillemotleft}}1
{»}{{\guillemotright}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1 {¡}{{!`}}1,
}
\lstset{escapechar=@,style=solarizedcsharp}
\begin{document}
\section{This appears normal}
\blindtext
\section{Code Here}
\begin{lstlisting}
var a = "Dummy Code!" 1234
\end{lstlisting}
\section{This has different styling!}
\blindtext
\end{document}
当然,另一个解决方案是保留\symbolstyle
并修复用法宏。但这将需要对 MWE 进行更多更改。