Error de paquete xcolor: color no definido `gray45'. error al usar lstlistings en LaTeX

Error de paquete xcolor: color no definido `gray45'. error al usar lstlistings en LaTeX

Estoy trabajando con LaTeX en el dorso y tengo el siguiente problema:

Cuando pongo este fragmento de código:

\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}

Recibo un error:

Package xcolor Error: Undefined color `gray45'.

Ese error se muestra en la línea:

#hostname

Realmente no entiendo lo que está pasando. ¿Alguien podría decirme, por favor?

Respuesta1

No sé dónde surge realmente tu problema.

No debe cargar el colorpaquete tres veces (dos veces como color, una vez como xcolor). Quité dos de ellos. Además, a tu MWE le falta un \documentclassy \begin{document}... \end{document}Los agregué y todo está bien para mí.

\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}

ingrese la descripción de la imagen aquí

Respuesta2

El error se solucionó definiendo un color llamado 'gray45', así:

\definecolor{gray45}{rgb}{0.2, 0.5, 0.478}

Parece que al ingresar un carácter # en un cuadro de listado, se vincula a compilarlo como un color, cuando no lo es. Pero como dije, definiendo el color el error se solucionó.

información relacionada