0

I am working with LaTeX in Overleaf, and I have the next problem:

When I put this code fragment:

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

I am getting an error:

Package xcolor Error: Undefined color `gray45'.

That error is shown in line:

#hostname

I don't really understand what is happening. Could anyone tell me, please?

2
  • When I remove # character, all seems to be fine... – jartymcfly Jan 24 '17 at 8:30
  • You're writing in python where # indicates a comment, and gray45 is set up as your comment colour -- so making text not a comment would stop the error but spoil the listing. – Chris H Jan 24 '17 at 8:50
2

I don't know, where your problem actually arises.

You should not load the color-package three times, (two times as color, one time as xcolor). I removed two of them. Also, you MWE lacks a \documentclass and \begin{document} ... \end{document}. I added them and everything is fine to me.

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

enter image description here

2

The error was solved defining a color named 'gray45', like this:

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

It seems that when entering a character # into a listings box, it ties to compile it as a color, when it is not. But, as I said, defining the color the error was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.