Use listings
package.
Simple configuration for LaTeX header (before \begin{document}
):
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
You can change default language in the middle of document with \lstset{language=Java}
.
Example of usage in the document:
\begin{lstlisting}
// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class Hello extends JApplet {
public void paintComponent(Graphics g) {
g.drawString("Hello, world!", 65, 95);
}
}
\end{lstlisting}
Here's the result:
Depending on why you want to do this, you might be better off using the built-in TeX support for hanging indentation, which the hanging package sugars. If it's a one-off, use the package, but if it's built in to some other layout, the package might just confuse things. Up to you.
The \hangindent
dimension gives the size of the indentation, and the \hangafter
number indicates when that indentation should start or stop. If the \hangafter
number is positive, then indentation starts after that number of lines; if it's negative, then it starts immediately and stops after (minus) that many lines.
These apply only to the immediately following paragraph. The hanging package handles this by using \everypar
. That's an occasionally problematic technique, which is why the package includes a 'word of caution' about it.
\documentclass{article}
\parindent=0pt
\parskip=\medskipamount
\begin{document}
\hangindent=2em
\hangafter=2
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Proin eu tempor velit. Fusce accumsan ultrices fringilla. Praesent
sed odio mi. Mauris non ligula turpis. Duis posuere lacus nec diam
interdum dictum suscipit magna molestie. Vestibulum nibh dolor,
interdum eget rhoncus ut, sodales eget justo. Morbi blandit lorem
sit amet nulla egestas aliquam. Nunc pharetra est at nibh ullamcorper
in commodo erat dignissim. Cras et suscipit enim.
\hangindent=2em
\hangafter=-2
Nunc adipiscing ligula at ligula egestas id ullamcorper felis luctus.
Aliquam tincidunt turpis sed eros pellentesque iaculis. Nulla
imperdiet cursus enim condimentum congue.
\end{document}
Best Answer
LaTeX will usually not indent the first paragraph of a section. This is standard typographical practice. However, if you really want to override this default setting, use the package indentfirst available on CTAN.