[Tex/LaTex] Figure environment not working with included file (extra } or forgotten endgroup)

colorfloatsincludeprogramming

I've spent hours on this issue. I hope someone can help me. I'm running into some kind of interference when I try to use the figure environment (Likewise for table, might be related to floats). I finally narrowed the problem down to one line– where I include another file in the preamble that allows me to later include python code and have Latex color code it. The code I'm trying to compile is:

\documentclass[10pt]{article}
\include{pythonperfect}
\usepackage{graphicx}
\begin{document}
\begin{figure}
 blah blah blah
\end{figure}
\end{document}

where "pythonperfect" is:

\usepackage{color}
\usepackage[procnames]{listings}
\usepackage{textcomp}
\usepackage{setspace}
\usepackage{palatino}
\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\definecolor{green}{rgb}{0,0.5,0}
\definecolor{lightgreen}{rgb}{0,0.7,0}
\definecolor{purple}{rgb}{0.5,0,0.5}
\definecolor{darkred}{rgb}{0.5,0,0}
\definecolor{orange}{rgb}{1,0.5,0}
%\lstnewenvironment{python}[1][]{
\lstset{
language=python,
basicstyle=\ttfamily\small\setstretch{1},
stringstyle=\color{green},
showstringspaces=false,
alsoletter={1234567890},
otherkeywords={\ , \}, \{},
keywordstyle=\color{blue},
emph={access,and,as,break,class,continue,def,del,elif,else,%
except,exec,finally,for,from,global,if,import,in,is,%
lambda,not,or,pass,print,raise,return,try,while,assert},
emphstyle=\color{orange}\bfseries,
emph={[2]self},
emphstyle=[2]\color{gray},
emph={[4]ArithmeticError,AssertionError,AttributeError,BaseException,%
DeprecationWarning,EOFError,Ellipsis,EnvironmentError,Exception,%
False,FloatingPointError,FutureWarning,GeneratorExit,IOError,%
ImportError,ImportWarning,IndentationError,IndexError,KeyError,%
KeyboardInterrupt,LookupError,MemoryError,NameError,None,%
NotImplemented,NotImplementedError,OSError,OverflowError,%
PendingDeprecationWarning,ReferenceError,RuntimeError,RuntimeWarning,%
StandardError,StopIteration,SyntaxError,SyntaxWarning,SystemError,%
SystemExit,TabError,True,TypeError,UnboundLocalError,UnicodeDecodeError,%
UnicodeEncodeError,UnicodeError,UnicodeTranslateError,UnicodeWarning,%
UserWarning,ValueError,Warning,ZeroDivisionError,abs,all,any,apply,%
basestring,bool,buffer,callable,chr,classmethod,cmp,coerce,compile,%
complex,copyright,credits,delattr,dict,dir,divmod,enumerate,eval,%
execfile,exit,file,filter,float,frozenset,getattr,globals,hasattr,%
hash,help,hex,id,input,int,intern,isinstance,issubclass,iter,len,%
license,list,locals,long,map,max,min,object,oct,open,ord,pow,property,%
quit,range,raw_input,reduce,reload,repr,reversed,round,set,setattr,%
slice,sorted,staticmethod,str,sum,super,tuple,type,unichr,unicode,%
vars,xrange,zip},
emphstyle=[4]\color{purple}\bfseries,
upquote=true,
morecomment=[s][\color{lightgreen}]{"""}{"""},
commentstyle=\color{red}\slshape,
literate={>>>}{\textbf{\textcolor{darkred}{>{>}>}}}3%
         {...}{{\textcolor{gray}{...}}}3,
procnamekeys={def,class},
procnamestyle=\color{blue}\textbf,
%framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox,
%rulesepcolor=\color{blue},
breaklines=true,
}{}

The error I get is:

! Too many }'s.
\color@endbox->\color@endgroup \egroup
l.10 \end{figure}
You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.
! LaTeX Error: \begin{document} ended by \end{figure}.

In any case, I've used "pythonperfect" for a while, and never ran into an issue until today when I tried to include a figure. The problem is that I don't really understand most of what's happening in that file, I found it somewhere on the web (I don't remember where) and made a few changes to make it work for me. But I don't really understand it. So I can't figure out what's wrong.

I'd really appreciate any help. Thank you very much.

Best Answer

As Martin Scharrer mentioned in his comment, the example works. However, I think the problem starts in the moment you use the newly defined environment \lstnewenvironment.

There was a closing } missing and when you used

\begin{python}

\end{python}

the error message came up. Have a look at this file's very last line. I also un-commented the new environment definition.

\usepackage{color}
\usepackage[procnames]{listings}
\usepackage{textcomp}
\usepackage{setspace}
\usepackage{palatino}
\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\definecolor{green}{rgb}{0,0.5,0}
\definecolor{lightgreen}{rgb}{0,0.7,0}
\definecolor{purple}{rgb}{0.5,0,0.5}
\definecolor{darkred}{rgb}{0.5,0,0}
\definecolor{orange}{rgb}{1,0.5,0}
\lstnewenvironment{python}[1][]{%%%% Included this
\lstset{
language=python,
 basicstyle=\ttfamily\small\setstretch{1},
 stringstyle=\color{green},
 showstringspaces=false,
 alsoletter={1234567890},
 otherkeywords={\ , \}, \{},
 keywordstyle=\color{blue},
 emph={access,and,as,break,class,continue,def,del,elif,else,%
 except,exec,finally,for,from,global,if,import,in,is,%
 lambda,not,or,pass,print,raise,return,try,while,assert},
 emphstyle=\color{orange}\bfseries,
 emph={[2]self},
 emphstyle=[2]\color{gray},
 emph={[4]ArithmeticError,AssertionError,AttributeError,BaseException,%
DeprecationWarning,EOFError,Ellipsis,EnvironmentError,Exception,%
False,FloatingPointError,FutureWarning,GeneratorExit,IOError,%
ImportError,ImportWarning,IndentationError,IndexError,KeyError,%
KeyboardInterrupt,LookupError,MemoryError,NameError,None,%
NotImplemented,NotImplementedError,OSError,OverflowError,%
PendingDeprecationWarning,ReferenceError,RuntimeError,RuntimeWarning,%
StandardError,StopIteration,SyntaxError,SyntaxWarning,SystemError,%
SystemExit,TabError,True,TypeError,UnboundLocalError,UnicodeDecodeError,%
UnicodeEncodeError,UnicodeError,UnicodeTranslateError,UnicodeWarning,%
UserWarning,ValueError,Warning,ZeroDivisionError,abs,all,any,apply,%
basestring,bool,buffer,callable,chr,classmethod,cmp,coerce,compile,%
complex,copyright,credits,delattr,dict,dir,divmod,enumerate,eval,%
execfile,exit,file,filter,float,frozenset,getattr,globals,hasattr,%
hash,help,hex,id,input,int,intern,isinstance,issubclass,iter,len,%
license,list,locals,long,map,max,min,object,oct,open,ord,pow,property,%
quit,range,raw_input,reduce,reload,repr,reversed,round,set,setattr,%
slice,sorted,staticmethod,str,sum,super,tuple,type,unichr,unicode,%
vars,xrange,zip},
 emphstyle=[4]\color{purple}\bfseries,
 upquote=true,
 morecomment=[s][\color{lightgreen}]{"""}{"""},
 commentstyle=\color{red}\slshape,
 literate={>>>}{\textbf{\textcolor{darkred}{>{>}>}}}3%
          {...}{{\textcolor{gray}{...}}}3,
 procnamekeys={def,class},
 procnamestyle=\color{blue}\textbf,
framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox,
rulesepcolor=\color{blue},
 breaklines=true,
 #1
}}{}%%%%Here it was missing }

And the example

\documentclass[10pt]{article}
\input{pythonperfect}%input better than include here
\usepackage{graphicx}
\begin{document}
\begin{figure}
 blah blah blah
\end{figure}

\begin{python}
 for a
\end{python}

\end{document}