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}}
listings
package be easier to use?