我正在 Overleaf 中使用 LaTeX,遇到下一個問題:
當我放置此程式碼片段時:
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage[usenames, dvipsnames]{color}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{color}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
basicstyle=\footnotesize\ttfamily, % 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
commentstyle=\color[rgb]{0.4,0.4,0.4}, % comment style
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
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[rgb]{0.18,0.28,0.75}, % keyword style
language=Python, % the language of the code
numberstyle=\color[rgb]{0.7,0.1,0.4}, % 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))
stringstyle=\color[rgb]{1,0.6,0.2}, % string literal style
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
tabsize=2, % sets default tabsize to 2 spaces
}
\begin{lstlisting}[language=bash,caption={Establecer hostname}]
#hostname
\end{lstlisting}
我收到錯誤:
Package xcolor Error: Undefined color `gray45'.
該錯誤如行所示:
#hostname
我真的不明白發生了什麼事。請問有人可以告訴我嗎?
答案1
我不知道你的問題實際上出現在哪裡。
您不應載入color
-package 三次(兩次作為color
,一次作為xcolor
)。我刪除了其中兩個。另外,您的 MWE 缺少一個\documentclass
和\begin{document}
... \end{document}
。我添加了它們,一切對我來說都很好。
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage[usenames, dvipsnames]{color}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
basicstyle=\footnotesize\ttfamily, % 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
commentstyle=\color[rgb]{0.4,0.4,0.4}, % comment style
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
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[rgb]{0.18,0.28,0.75}, % keyword style
language=Python, % the language of the code
numberstyle=\color[rgb]{0.7,0.1,0.4}, % 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))
stringstyle=\color[rgb]{1,0.6,0.2}, % string literal style
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
tabsize=2, % sets default tabsize to 2 spaces
}
\begin{document}
\begin{lstlisting}[language=bash,caption={Establecer hostname}]
#hostname
\end{lstlisting}
\end{document}
答案2
透過定義名為「gray45」的顏色解決了該錯誤,如下所示:
\definecolor{gray45}{rgb}{0.2, 0.5, 0.478}
似乎當在列錶框中輸入字元 # 時,它會將其編譯為顏色,而事實並非如此。但是,正如我所說,定義顏色錯誤已解決。