# Centering left justified text

I need to define a new environment that contains a minipage centered on the current page and with text left-justified.

Something like:

\newenvironment{texto}{
\begin{center} % so the minipage is centered
\begin{minipage}[t][\textwidth]
\begin{flushleft} % so the minipage's text is left justified
}{
\end{flushleft}
\end{minipage}
\end{center}
}


I've been trying for hours without luck so far.

If it was possible to put the minipage inside a centered makebox inside the new environment it would be possible, but there's no makebox environment.

EDIT:

\documentclass[a4paper,10pt,twoside,onecolumn,openright,final]{book}
\newenvironment{code-block}{
\begin{center}
\begin{minipage}[t]{\textwidth}
\raggedright
}{
\end{minipage}
\end{center}
}

\begin{document}

a completely random line, filled with random words, or perhaps not!

\begin{code-block}
pineapples with mustard
\end{code-block}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}


result:

-

To allow for a variable-width minipage environment, you need the varwidth package. From the package documentation:

The package deﬁnes a varwidth environment (based on minipage) which is an analogue of minipage, but whose resulting width is the natural width of its contents.

The minimal example as presented in the package documentation gives a view on what means:

So, alter Stefan's answer in the following way:

\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\newenvironment{texto}{%
\begin{center} % so the minipage is centered
\begin{varwidth}[t]{\textwidth}
\raggedright % so the minipage's text is left justified
}{%
\end{varwidth}
\end{center}
}


-
It works perfectly, thanks! –  joca Aug 12 '11 at 23:45
@joca: Nice to see that Werner's solution works for you! I've noticed, you accepted his answer, it would also be great if you vote it up as you saw it's useful, just by clicking the arrow on the left of his post - I did. –  Stefan Kottwitz Aug 13 '11 at 10:04
I tried, but I am not registered. –  joca Aug 14 '11 at 20:54

Here's a suggestion:

\newenvironment{texto}[1][\textwidth]{
\begin{center} % so the minipage is centered
\begin{minipage}[t]{#1}
\raggedright % so the minipage's text is left justified
}{
\end{minipage}
\end{center}
}

• I used the declaration \raggedright instead of the flushleft environment since the effect is limited by the outer minipage environment.

• I introduced an optional argument for the mandatory width of the minipage.

A complete example:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\newenvironment{texto}[1][\textwidth]{
\begin{center} % so the minipage is centered
\begin{minipage}[t]{#1}
\raggedright % so the minipage's text is left justified
}{
\end{minipage}
\end{center}
}

\begin{document}
\blindtext
\begin{texto}[6cm]
\blindtext
\end{texto}
\blindtext
\end{document}


-
I've tried that, it doesn't work. –  joca Aug 12 '11 at 20:13
@joca: you could be more specific. I'll add a minimal example showing that it does work. –  Stefan Kottwitz Aug 12 '11 at 20:23
See my edit, my minimal example shows it does not work. –  joca Aug 12 '11 at 22:51
@joca: Your minimal example does work. You are typesetting a minipage of width \textwidth (by default, since you don't specify any other length like in Stefan's answer) and justify it \raggedright. Specifying a minipage of width \textwidth will produce something that looks similar to just removing the minipage altogether, resulting in exactly what you presented. That is, centering a minipage of width \textwidth does nothing visually. Do you want to have a modified width for the minipage? –  Werner Aug 12 '11 at 23:09
Ok, I'm sorry then. I want the minipage to scale to contain the text inside it (it will contain verbatim-like text), I don't know the dimension before hand. Sometimes it can be 6cm, 4cm, etc. –  joca Aug 12 '11 at 23:15