3

I want to create an environment which makes some text look like code. Namely, I want the font to be monospace, with a light grey background (precisely, \definecolor{mygray}{gray}{0.9}). I also need it to have proper indentation. However, I do not want to use either the listings or minted packages for other reasons, and I do not need automatic highlighting of code in fancy colours (I want to do these manually---using \textbf and other LaTeX commands should still be possible within this environment, instead of it being displayed as code). In essence, within this environment everything works precisely the same as normal text, except the font, the background colour, the font-size which I want to be \scriptsize, and automatic indenting (similar to listings). Properties like text wrap should be conserved.

Here's where I got so far:

\documentclass{article}
\usepackage[scaled=0.85]{beramono}

\newenvironment{monospace}{\ttfamily}{\par}
\newenvironment{code}{\begin{monospace}\begin{tabbing}}{\end{tabbing}\end{monospace}}

\begin{document}

\begin{code}
\textbf{def} \=weird(x,y):\\
    \>  a = math.sin(x)\\
    \>  b = math.cos(y)\\
    \>  c = math.tan(x+y)\\
    \>  s = (a+b+c)/3\\
    \>  \textbf{def} \=tant(n):\\
    \>  \>  \textbf{return} math.tan((n+x+y)/3)\\
    \>  \textbf{return} \=math.cos(math.sin(tant(s)*\\
    \>  \>       tant(a)*tant(b)*tant(c)))
\end{code}

\end{document}

The problem with this, is of course that creating the correct indentation is extremely tedious, and text wrap has to be done manually (the last statement is broken up into two lines, because I'm using twocolumn mode and without the manual line break, the code exceeds the margin). Also, I have no clue how to add the background colour. Any help is appreciated! enter image description here

  • I know it may seem annoying, but could you explain exactly why you do not want to use verbatim, minted or listings ? – BambOo Aug 10 '18 at 12:58
  • I am writing a report, and most unfortunately my advisor detests the use of these environments, for some arbitrary reason. He just doesn't like it, and I have to comply. Also, verbatim does not recognize \textbf as a command and keeps it as output, which is not what I want. I realise there is most certainly a way to circumvent this, but I just think making a new environment is easier. – Teyyf Aug 10 '18 at 14:13
  • You could try something based on an algorithm package such as algorithm2e It already has automatic indentation and you can define your own keywords. – BambOo Aug 10 '18 at 14:21
4

It will probably be simplest to use fancyvrb, with fvextra if you want automatic line breaking. You might start with something like this:

\documentclass[twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{xcolor}
\definecolor{mygray}{gray}{0.9}
\usepackage{fvextra}
\usepackage{tcolorbox}

\newenvironment{code}%
 {\VerbatimEnvironment
  \begin{tcolorbox}[colback=mygray, boxsep=0pt, arc=0pt, boxrule=0pt]
  \begin{Verbatim}[fontsize=\scriptsize, commandchars=\\\{\},
    breaklines, breakafter=*, breaksymbolsep=0.5em,
    breakaftersymbolpre={\,\tiny\ensuremath{\rfloor}}]}%
 {\end{Verbatim}%
  \end{tcolorbox}}

\begin{document}

\begin{code}
\textbf{def} weird(x,y):
    a = math.sin(x)
    b = math.cos(y)
    c = math.tan(x+y)
    s = (a+b+c)/3
    \textbf{def} tant(n):
        \textbf{return} math.tan((n+x+y)/3)
    \textbf{return} math.cos(math.sin(tant(s)*tant(a)*tant(b)*tant(c)))
\end{code}

\end{document}

enter image description here

|improve this answer|||||
  • Just what I wanted! One small problem: the spacing between the last and second last lines (the text-wrapped line) seems to be excessive, is there any way to reduce it? – Teyyf Aug 11 '18 at 1:21
  • @Teyyf That's due to the default end-of-line break symbol not working with such a small font size. You could define a new symbol based on \rfloor, or just remove it altogether with breakaftersymbolpre={} in the Verbatim options. – G. Poore Aug 11 '18 at 11:40
  • I'm sorry, but I have no idea how to do this. Could you update your answer to add that in? – Teyyf Aug 11 '18 at 13:58
  • @Teyyf It's updated. – G. Poore Aug 11 '18 at 14:22
2

You can insert source code in LaTeX using the verbatim package or the listing package, that i prefer, because it gives you more options.

Here is the code you need :

\documentclass{article}

\usepackage{listings}

\begin{document}

\begin{lstlisting}[language=python]
def weird(x,y):
    a = math.sin(x)
    b = math.cos(y)
    c = math.tan(x+y)
    s = (a+b+c)/3
def tant(n):
    return math.tan((n+x+y)/3)
    return math.cos(math.sin(tant(s)*
        tant(a)*tant(b)*tant(c)))
\end{lstlisting}

\end{document}

There are many other option for the language, and you can even create your own.

|improve this answer|||||
  • Sorry, but as mentioned before, I do not want to use this method due to other reasons. Another approach would be very much appreciated. – Teyyf Aug 10 '18 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.