# Set width of a custom environment

I'm trying to make a letter template (I don't want to use the existing one) and I am trying to make an environment for the adress at the top. I want it to be a certain width and located to the right part of the page, but aligned left.

I can not figure out how to do it. Creating custom layouts and templates overall in LaTeX has brought me much headache.

I tried to do something like this:

\newenvironment{head}
{\leftskip=2cm}
{\leftskip=0cm}


But it doesn't work. :/

-

It doesn't work because of many facts. First of all, TeX uses only one value for \leftskip, precisely what is current at the end of the paragraph. Secondly, your definition hides the value given to \leftskip, because every environment forms a group: so when TeX finds the end of the paragraph, it will have already forgotten the setting to \leftskip. If you want to typeset a paragraph with a non zero \leftskip you should define

\newenvironment{head}
{\par\setlength{\leftskip}{2cm}\noindent\ignorespaces}
{\par}


The first \par ends the previous text; then we set \leftskip and then apply \noindent (which requires \ignorespaces to ignore the end of line after \begin{head}).

At the end we issue \par to end the paragraph which will be typeset with the stated setting of \leftskip. There's no need to reset \leftskip to zero, because that's already taken care of by the end of the implicit group formed by the environment.

## Note

It's better to stick to \setlength, because \leftskip is a glue parameter; there's an example in the TeXbook: try and define

\newenvironment{badidea}
{\par\leftskip=2cm}
{\par}


and write

\begin{badidea}
minuscule chances of error


You'll have a surprise. :)

-

\documentclass{article}
\begin{document}
\hfill
\begin{minipage}{0.5\linewidth}
goes here and is\\
appropriately\\
aligned
\end{minipage}
\end{document}


As an environment:

\newenvironment{letteraddress}{\hfill\begin{minipage}{0.5\linewidth}}{\end{minipage}}


This of course assumes that \linewidth is actually defined. You could always just put in an explicit width if you prefer...

-

Your question was answered already in Set width of algorithm environment (preferably document wide). It is simply using minipages.

The final code should look something like this:

\documentclass{article}
{
\centering
\begin{flushright}
\begin{minipage}{5cm}}%
{
\end{minipage}
\end{flushright}}

\begin{document}