Text Formattingmoreverb | Code with Colors

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

moreverb | Code with Colors

Post by Cham »

I'm currently using the moreverb package to insert some Mathematica code in a LaTeX document. Everything is fine in black and white. But is it possible to add code colors as well ?

More precisely, suppose we want to show a piece of computer code in a LaTeX document. The computer code has some parts in colors. How can we tell LaTeX to show the code colors ?

(hmmm, sorry if my English sounds terrible here. I'm doing my best... :oops: )

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moreverb | Code with Colors

Post by localghost »

For code in a certain programming language you are better with the listings or minted package. A forum search for sure yields usable results.


Thorsten
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: moreverb | Code with Colors

Post by Cham »

Hmmm, can you give a small example of a code snippet with some colors ?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moreverb | Code with Colors

Post by localghost »

Cham wrote:Hmmm, can you give a small example of a code snippet with some colors ?
Actually it is your homework to submit a small example on which we can test possible solutions. As already mentioned, a search in the forum surely will bring some helpful examples. First sources of information would be the manuals of the above mentioned packages.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

moreverb | Code with Colors

Post by Cham »

Ok, here's a working example to be edited. It's just a small code snippet from Mathematica. I would like to have it in Mathematica's default typesetting, with colors.

Code: Select all

\documentclass[ ]{revtex4}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{listings}
\pagestyle{plain}

\begin{document}

\begin{lstlisting}
Mu0[t_, x_, y_, z_] := {
		Cos[2Pi t - k(r[x, y, z])]Sin[alpha],
		Sin[2Pi t - k(r[x, y, z])]Sin[alpha],
		Cos[alpha]
		}
\end{lstlisting}

\end{document}
Currently, this example isn't satisfying.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

moreverb | Code with Colors

Post by cgnieder »

Have you taken a look into the manual? There are quite some options to customize the output.

Code: Select all

\documentclass[ ]{revtex4}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{listings,xcolor}
\pagestyle{plain}

\begin{document}

\begin{lstlisting}[language=mathematica,basicstyle=\ttfamily,keywordstyle=\color{blue}]
Mu0[t_, x_, y_, z_] := {
      Cos[2Pi t - k(r[x, y, z])]Sin[alpha],
      Sin[2Pi t - k(r[x, y, z])]Sin[alpha],
      Cos[alpha]
      }
\end{lstlisting}

\end{document}
Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: moreverb | Code with Colors

Post by Cham »

Ahaa, this becomes interesting.

The manual is extremelly heavy and cryptic for me. I don't know how to use it. Is there a manual for the manual ? :oops:
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: moreverb | Code with Colors

Post by Cham »

Thanks a lot, cgnieder !

I'm beginning to understand a few things from the manual. The results are already great !
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

moreverb | Code with Colors

Post by Cham »

Now, I think the font used (ttfamily) is too large for the listing, but I want to keep that font style. How can I reduce it to, say, 10 points ?

Or maybe use the default size of the whole document (12 points) ? How ?

Currently, the ttfamily is too large inside the listing.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

moreverb | Code with Colors

Post by Cham »

Now, what is wrong with this code ? It's working, but I'm unable to get the ttfamily font :

Code: Select all

\documentclass[ ]{revtex4}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{listings,xcolor}
\pagestyle{plain}

\begin{document}

This is a test :

\lstset{tabsize=4,frame=single,language=mathematica,stringstyle=\ttfamily,basicstyle=\scriptsize,keywordstyle=\color{blue},backgroundcolor=\color{lightgray},
emph={Mu0},emphstyle=\color{red},
emph={[2]alpha,k},emphstyle={[2]\color{green}},
emph={[3]r},emphstyle={[3]\color{blue}}}
\begin{lstlisting}[title={A title}]
	Mu0[t_, x_, y_, z_] :=
		{
		Cos[2Pi t - k(r[x, y, z])]Sin[alpha],
		Sin[2Pi t - k(r[x, y, z])]Sin[alpha],
		Cos[alpha]
		}
\end{lstlisting}

\end{document}
Also, how can I remove the subtle white lines on the background ?
Post Reply