2

I'm trying to use the listings package to format my code to look like Java code. This is MWE I try to use:

\documentclass[a4paper,11pt]{report} 

\usepackage[utf8]{inputenc} % utf8
\usepackage[T1]{fontenc} 
\usepackage{xcolor}

\usepackage{listings}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}
\definecolor{annotation}{cmyk}{0, 0, 1, 0.2}

\lstdefinestyle{Java}{  
    language=Java, %if declared outside, causes problems
    showspaces=false,
    showtabs=false,
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    commentstyle=\color{pgreen},
    keywordstyle=\color{pblue},
    stringstyle=\color{pred},
    basicstyle=\ttfamily\singlespacing,
    moredelim=[il][\textcolor{annotation}]{\$\$},
    moredelim=[is][\textcolor{annotation}]{\%\%}{\%\%},
    rulecolor= \color{black} 
}

\begin{document}
    \lstset{style=Java}
    \begin{lstlisting}
        public byte getSelectedPortAsByte(){
            int temp;

            switch(selectedPort){
                case "PORTB":
                    temp= 0x01;
                    break;
                case "PORTC":
                    temp= 0x02;
                    break;
                case "PORTD":
                    temp= 0x03;
                    break;
                default:
                    temp= 0x00;
                break;
            }
            return (byte)temp;
        }
    \end{lstlisting}
\end{document}

When trying to build it, I get the following error message: line 34: Undefined control sequence. \begin{lstlisting} I'm using TexStudio 2.12.6, Win7, MikTeX 2.9.

Am I missing something obvious here?

3

Unfortunately this is a well known failing of the editor misleading you, the error message is

! Undefined control sequence.
\lst@basicstyle ->\ttfamily \singlespacing 

as single spacing is not defined (setspace package required perhaps) the editor just shows the least useful, final line of context where tex detected the error

l.33     \begin{lstlisting}

https://sourceforge.net/p/texstudio/feature-requests/1068/

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.