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 am going to include some scripts (programming) in my document and I used \texttt to make them look different from the rest of the document and I am gonna move them to the center of page with center environment but I don't have any clue how to align them (like the alignment used for equations):

\begin{center}
\texttt{kmeans=: 3 :0 \\
Nr=: \$,smp \\
dis=: \%: @ +/"1(initial -"1/ grid)\^ 2 \\
index=:(|:dis) i."1 (,.<./"1 (|:dis)) \\
}
\end{center}
share|improve this question
add comment

1 Answer

If you're not interested in a specific formatting of the scripts, then you can use the fancyvrb package to adjust the code location. Here's an example that indents the entire code by 2\parindent (twice the standard \parindent) for a consistent horizontal alignment across scripts:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\fvset{xleftmargin=2\parindent}
\begin{document}
\lipsum[1]
\begin{Verbatim}
kmeans=: 3 :0
Nr=: $,smp
dis=: %: @ +/"1(initial -"1/ grid)^ 2
index=:(|:dis) i."1 (,.<./"1 (|:dis))
\end{Verbatim}
\lipsum[2]
\end{document}

Using something like a verbatim environment (or Verbatim as provided by fancyvrb) removes the need to escape characters that are otherwise treated differently inside regular text. Moreover, the default font used it \ttfamily (this can be changed).


For actually centering the content, fancyvrb also allows you to capture the content in a box which provides the means to move it around:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\lipsum[1]
\begin{SaveVerbatim}{code}
kmeans=: 3 :0
Nr=: $,smp
dis=: %: @ +/"1(initial -"1/ grid)^ 2
index=:(|:dis) i."1 (,.<./"1 (|:dis))
\end{SaveVerbatim}
\begin{center}
  \fcolorbox{red!60!black}{white}{\BUseVerbatim{code}}
\end{center}
\lipsum[2]
\end{document}​

For formatting options, you should consider using the listings package.

share|improve this answer
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.