2

Edit: thank you for commenting. I've edited the question to use lstnewenvironment but it keeps giving the same error. I will keep the question here while I go read the lstlistings manual.

The following code works for me when used inside the document:

\begin{adjustwidth*}{0pt}{-5cm}
\begin{lstlisting}
function test() { return (void *) test_func(a, b, 3); }
\end{lstlisting}
\end{adjustwidth*}

But when I want to create a custom environment for it, Overleaf gives me the error

job aborted, no legal \end found.

MWE:

\documentclass{article}
\usepackage[right=10cm]{geometry}    % Create a large right margin
\usepackage{listings}
\usepackage{changepage}

% Create a new environment for code samples that can extend into the outer margin. \lstnewenvironment{config}{% \begin{adjustwidth}{0pt}{-5cm} }{% \end{adjustwidth} }

\begin{document} \begin{config} function test() { return (void *) test_func(a, b, 3); } asdfasdfa asdf \end{config} \end{document}

epR8GaYuh
  • 2,432
Tommiie
  • 526
  • 1
    The problematic part here is not the adjustwidth environment but lstlisting. \newenvironment{config}{\begin{lstlisting}}{\end{lstlisting}} causes the same error message that you observewith your original definition of the config environment. – leandriis Jul 21 '20 at 07:08
  • 1
    In order to create a new environment containing a lstlisting, the listings package offers \lstnewenvironment. See also: lstlisting in a newenvironment – leandriis Jul 21 '20 at 07:09

1 Answers1

1

Apparently, changepage and listings fight each other. However, you don't need changepage for this application.

\documentclass{article}
\usepackage[right=10cm]{geometry}    % Create a large right margin
\usepackage{listings}
\usepackage{lipsum}

% Create a new environment for code samples that can extend into the outer margin. \lstnewenvironment{config}[1][]{\lstset{xrightmargin=-5cm,#1}}{}

\lstset{basicstyle=\ttfamily,columns=fullflexible} % so my eyes don't bleed

\begin{document}

\lipsum[1][1-4]

\begin{config} function test() { return (void *) test_func(a, b, 3); } asdfasdfa asdf \end{config}

\end{document}

You can add listings options to config in the usual way, by calling `\begin{config}[]

enter image description here

egreg
  • 1,121,712