1

I'm using XeLaTeX and Overleaf as my editor. I want to use the listings package to list my LaTeX codes. I can properly list most normal codes using the my style below, but when I try to list some lines in my preamble, there would be a lot of errors. (e.g. Undefined control sequence. LaTeX Error: Environment lstlisting undefined. LaTeX Error: Missing \begin{document}. ...etc)

Are there any ways to list the \documentclass without corrupting the PDF? Should I use \verb instead?

Here is my code:

\documentclass[11pt, a4paper]{article}
\usepackage{listings,xcolor}

\definecolor{code-keyword-color}{rgb}{0.61, 0.14, 0.58} % purple
\definecolor{code-comment-color}{rgb}{0.36, 0.42, 0.47} % gray
\definecolor{code-string-color}{rgb}{0.77, 0.1, 0.09} % orange
\definecolor{code-identifier-color}{rgb}{0.42, 0.21, 0.66} %purple


\lstdefinestyle{mystyle}{
    basicstyle=\ttfamily\footnotesize,        
    numbers=left,                             % Position of line numbers
    numberstyle=\sffamily\scriptsize,         % Style of line numbers
    stepnumber=1,                             % Margin between line numbers
    numbersep=5pt,                            % Margin between line numbers and text
    tabsize=2,                                
    extendedchars=true,                       % Lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
    breaklines=true,                          % Lines will be wrapped
    breakatwhitespace=false,                  % Sets if automatic breaks should only happen at whitespace     
    keepspaces=true,                          % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
    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
    captionpos=t,                             % Caption position: top
    escapeinside={\%*}{*)},                   % If you want to add LaTeX within your code
    backgroundcolor=\color{white},
    commentstyle=\itshape\color{code-comment-color},
    keywordstyle=\color{code-keyword-color},
    identifierstyle=\color{code-identifier-color},
    stringstyle=\color{code-string-color},
    language=[LaTeX]TeX
}

\lstset{style=mystyle}

\begin{document}

\title{title}
\author{name}
\date{date}
\maketitle


% this works
\begin{lstlisting}
\begin{equation}
    E=mc^2
\end{equation}
\end{lstlisting}

% this does not work
\begin{lstlisting}
\documentclass[11pt, a4paper]{article}
\end{lstlisting}

\end{document}
1
  • Compile the file using xelatex+TeXLive2023, still no error occurs. image.
    – Stephen
    Commented Jul 12, 2023 at 5:02

1 Answer 1

2

This is due to the mechanics of how Overleaf choose files to compile.

When a main document is not specified in Overleaf, the compiler will try to compile the current file when "Compile" is executed.

When a main document is specified, Overleaf will still try to compile the current file if this document contains the string \documentclass, regardless which line it is, or whether it begins in a new line.

My workaround is to typeset this via \texttt{\textbackslash documentclass}, or \verb!\!\verb!documentclass!.

Reference: Overleaf - Set Main Document https://www.overleaf.com/learn/how-to/Set_Main_Document

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .