Custom environment for syntax highlighted and indented codeVertical space before and after custom...

In the UK, is it possible to get a referendum by a court decision?

Avoiding the "not like other girls" trope?

What is the most common color to indicate the input-field is disabled?

Forgetting the musical notes while performing in concert

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

How to install cross-compiler on Ubuntu 18.04?

Send out email when Apex Queueable fails and test it

Notepad++ delete until colon for every line with replace all

Processor speed limited at 0.4 Ghz

How does a dynamic QR code work?

How do conventional missiles fly?

How to remove border from elements in the last row?

Is there a hemisphere-neutral way of specifying a season?

Could the museum Saturn V's be refitted for one more flight?

Which ISO should I use for the cleanest image?

Why is the sentence "Das ist eine Nase" correct?

What exactly is ineptocracy?

Do Iron Man suits sport waste management systems?

Getting extremely large arrows with tikzcd

How to stretch the corners of this image so that it looks like a perfect rectangle?

Sums of two squares in arithmetic progressions

Can someone clarify Hamming's notion of important problems in relation to modern academia?

Unlock My Phone! February 2018

Rotate ASCII Art by 45 Degrees



Custom environment for syntax highlighted and indented code


Vertical space before and after custom environmentCustom environment problems using environTeXstudio: How to add custom highlighting for custom environment?Custom environment shorthand for multiple environmentsHide indented amsthm environment using comment packagehanging indent within indented texttt quote environmentSame indent for whole custom defined environment?Own equation environment for - R syntax formulasNew environment with a heading and an indented paragraphMaking a multiline code environment













0















I am writing some lecture notes involving blocks of code. I would like a custom environment always indents the block with consistent formatting. My current attempt fails:



usepackage{color}
usepackage{listings}

newenvironment{code}[1]%
{begin{lstlisting}
{setlength{leftmargin}{4em}}%
#1
end{lstlisting}
}

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}

lstset{language=Java,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
commentstyle=color{pgreen},
keywordstyle=color{pblue},
stringstyle=color{pred},
basicstyle=ttfamily,
moredelim=[il][textcolor{pgrey}]{$$},
moredelim=[is][textcolor{pgrey}]{%%}{%%}
}


When I try this in LaTeX, I get



begin{code}
if (int == 2) System.out.println("foo");
else System.out.println("bar");
end{code}


I get the error message:



*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
(./titlepage.tex
Underfull hbox (badness 10000) in paragraph at lines 4--5

[1{/usr/local/texlive/2018/texmf-var/fonts/map/pdftex/updmap/pdftex.map}])
[2]

LaTeX Font Warning: Font shape `OT1/cmr/bx/sc' undefined
(Font) using `OT1/cmr/bx/n' instead on input line 25.

(./main.toc) [3] [4] (./arrays.tex
Chapter 1.
! Missing endcsname inserted.
<to be read again>
protect
l.10 i
f (i == 3) System.out.println("foo");
?


My listing environment on its own works fine (e.g. syntax highlighting works).









share



























    0















    I am writing some lecture notes involving blocks of code. I would like a custom environment always indents the block with consistent formatting. My current attempt fails:



    usepackage{color}
    usepackage{listings}

    newenvironment{code}[1]%
    {begin{lstlisting}
    {setlength{leftmargin}{4em}}%
    #1
    end{lstlisting}
    }

    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}

    lstset{language=Java,
    showspaces=false,
    showtabs=false,
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    commentstyle=color{pgreen},
    keywordstyle=color{pblue},
    stringstyle=color{pred},
    basicstyle=ttfamily,
    moredelim=[il][textcolor{pgrey}]{$$},
    moredelim=[is][textcolor{pgrey}]{%%}{%%}
    }


    When I try this in LaTeX, I get



    begin{code}
    if (int == 2) System.out.println("foo");
    else System.out.println("bar");
    end{code}


    I get the error message:



    *geometry* driver: auto-detecting
    *geometry* detected driver: pdftex
    (./titlepage.tex
    Underfull hbox (badness 10000) in paragraph at lines 4--5

    [1{/usr/local/texlive/2018/texmf-var/fonts/map/pdftex/updmap/pdftex.map}])
    [2]

    LaTeX Font Warning: Font shape `OT1/cmr/bx/sc' undefined
    (Font) using `OT1/cmr/bx/n' instead on input line 25.

    (./main.toc) [3] [4] (./arrays.tex
    Chapter 1.
    ! Missing endcsname inserted.
    <to be read again>
    protect
    l.10 i
    f (i == 3) System.out.println("foo");
    ?


    My listing environment on its own works fine (e.g. syntax highlighting works).









    share

























      0












      0








      0








      I am writing some lecture notes involving blocks of code. I would like a custom environment always indents the block with consistent formatting. My current attempt fails:



      usepackage{color}
      usepackage{listings}

      newenvironment{code}[1]%
      {begin{lstlisting}
      {setlength{leftmargin}{4em}}%
      #1
      end{lstlisting}
      }

      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}

      lstset{language=Java,
      showspaces=false,
      showtabs=false,
      breaklines=true,
      showstringspaces=false,
      breakatwhitespace=true,
      commentstyle=color{pgreen},
      keywordstyle=color{pblue},
      stringstyle=color{pred},
      basicstyle=ttfamily,
      moredelim=[il][textcolor{pgrey}]{$$},
      moredelim=[is][textcolor{pgrey}]{%%}{%%}
      }


      When I try this in LaTeX, I get



      begin{code}
      if (int == 2) System.out.println("foo");
      else System.out.println("bar");
      end{code}


      I get the error message:



      *geometry* driver: auto-detecting
      *geometry* detected driver: pdftex
      (./titlepage.tex
      Underfull hbox (badness 10000) in paragraph at lines 4--5

      [1{/usr/local/texlive/2018/texmf-var/fonts/map/pdftex/updmap/pdftex.map}])
      [2]

      LaTeX Font Warning: Font shape `OT1/cmr/bx/sc' undefined
      (Font) using `OT1/cmr/bx/n' instead on input line 25.

      (./main.toc) [3] [4] (./arrays.tex
      Chapter 1.
      ! Missing endcsname inserted.
      <to be read again>
      protect
      l.10 i
      f (i == 3) System.out.println("foo");
      ?


      My listing environment on its own works fine (e.g. syntax highlighting works).









      share














      I am writing some lecture notes involving blocks of code. I would like a custom environment always indents the block with consistent formatting. My current attempt fails:



      usepackage{color}
      usepackage{listings}

      newenvironment{code}[1]%
      {begin{lstlisting}
      {setlength{leftmargin}{4em}}%
      #1
      end{lstlisting}
      }

      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}

      lstset{language=Java,
      showspaces=false,
      showtabs=false,
      breaklines=true,
      showstringspaces=false,
      breakatwhitespace=true,
      commentstyle=color{pgreen},
      keywordstyle=color{pblue},
      stringstyle=color{pred},
      basicstyle=ttfamily,
      moredelim=[il][textcolor{pgrey}]{$$},
      moredelim=[is][textcolor{pgrey}]{%%}{%%}
      }


      When I try this in LaTeX, I get



      begin{code}
      if (int == 2) System.out.println("foo");
      else System.out.println("bar");
      end{code}


      I get the error message:



      *geometry* driver: auto-detecting
      *geometry* detected driver: pdftex
      (./titlepage.tex
      Underfull hbox (badness 10000) in paragraph at lines 4--5

      [1{/usr/local/texlive/2018/texmf-var/fonts/map/pdftex/updmap/pdftex.map}])
      [2]

      LaTeX Font Warning: Font shape `OT1/cmr/bx/sc' undefined
      (Font) using `OT1/cmr/bx/n' instead on input line 25.

      (./main.toc) [3] [4] (./arrays.tex
      Chapter 1.
      ! Missing endcsname inserted.
      <to be read again>
      protect
      l.10 i
      f (i == 3) System.out.println("foo");
      ?


      My listing environment on its own works fine (e.g. syntax highlighting works).







      environments





      share












      share










      share



      share










      asked 7 mins ago









      gwggwg

      1011




      1011






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482875%2fcustom-environment-for-syntax-highlighted-and-indented-code%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482875%2fcustom-environment-for-syntax-highlighted-and-indented-code%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Installing LyX: “No textclass is found.”LyX installation error- text class not found- 'Reconfigure' or...

          (1602) Indiana Índice Designación y nombre Características orbitales Véase...

          Universidad Autónoma de Occidente Índice Historia Campus Facultades Programas Académicos Medios de...