[Tex/LaTex] How to automatically escape special characters in your own environment

environmentsverbatim

I need to create my environment for examples of source codes. I want it to look like this:

Code example:

————————————————

# This is the code example

echo 'Hello'

————————————————


The problem is, that I don't know, how to force it to escape special characters (hash especially) automatically, so the users don't have to type backslash every time they want to write the hash.


My code

\def\terminalText#1\end{\hspace*{2em}\texttt{#1}\\\end}

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\terminalText}
{\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}

This code is working well, untill I want to write any special character inside of the environment, then I'm getting this error:

! Paragraph ended before \terminalText was complete.


I've even tried to make some kind of wrapper to the verbatim environment, according to the question verbatim useable with a newenvironment definition?, but it's giving me this error:

! LaTeX Error: \begin{codeExample} on input line 535 ended by \end{verbatim}.

My 2nd code

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\verbatim
}
{%
\endverbatim
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}

Best Answer

Using verbatim package the code for the second example works out of the box (if dashrule package is included as well)

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}


\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}




\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\end{document}

enter image description here

Please consider listings package as a 'better' and cleaner way to typeset code examples.

Edit Here's an example with the tcolorbox output for listings.

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}

\usepackage[most]{tcolorbox}


\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}


\newtcblisting[auto counter]{codeex}[1][]{%
  arc=0pt,
  auto outer arc,
  colbacktitle=yellow,
  coltitle=black,
  title={Code Example \thetcbcounter},
  listing options={language=bash},
  listing only,
  lowerbox=ignored,
  before upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm},
  after upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm}
  #1
}

\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\begin{codeex}
echo 'Hello' 

\end{codeex}

\end{document}

enter image description here

Related Question