Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm submitting a project that involves blocks of code.

I figured a nice way to present it by be that paragraphs of code are black text on a light gray background, in courier font.

I'm wondering if it would be possible to create an environment, called "code" for example, such that,

\begin{code}
...
...
\end{code}

would have the gray background, and be in courier font.

Any ideas would be greatly appreciated!

share|improve this question
1  
What kind of code? You should consider the listings package. –  Peter Grill Apr 8 '12 at 3:25
add comment

1 Answer

up vote 3 down vote accepted

To use the courier font use,

 \renewcommand\ttdefault{pcr}

To define a code environment you can use the listings package.

 \documentclass{book}
%\usepackage[scaled=O.85]{luximono}
\usepackage{xcolor,listings}
\definecolor{theblue} {rgb}{0.02,0.04,0.48}
\definecolor{thered}  {rgb}{0.65,0.04,0.07}
\definecolor{thegreen}{rgb}{0.06,0.44,0.08}
\definecolor{thegrey} {gray}{0.5}
\definecolor{theshade}{gray}{0.94}
\definecolor{theframe}{gray}{0.75}

\renewcommand\ttdefault{pcr}

\lstloadlanguages{[LaTeX]TeX, [primitive]TeX}

\lstnewenvironment{code}[1][]
  {\lstset{language=[LaTeX]TeX}\lstset{%
      escapeinside={{(*@}{@*)}},
      breaklines=true,
      framesep=5pt,
      basicstyle=\ttfamily,
      showstringspaces=false,
      keywordstyle=\ttfamily\textcolor{thegreen},
      stringstyle=\color{orange},
       commentstyle=\color{black},
       rulecolor=\color{gray!10},
      breakatwhitespace=true,
      showspaces=false,  % shows spacing symbol
       xleftmargin=0pt,
       xrightmargin=5pt,
       aboveskip=0pt, % compact the code looks ugly in type
       belowskip=0pt,  % user responsible to insert any skips
      backgroundcolor=\color{gray!15}, #1
}}
{}

\begin{document}
\begin{code}
\let a=2+3
\end{code}
\end{document}
share|improve this answer
    
Looks beautiful! Thank you very much! –  AlmostSurely Apr 8 '12 at 16:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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