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}
목록 상자에 # 문자를 입력하면 색상이 아닌 경우 색상으로 컴파일하도록 연결되는 것 같습니다. 하지만 제가 말했듯이 색상을 정의하면 오류가 해결되었습니다.