[Tex/LaTex] Set width of a custom environment

environmentswidth

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. :/

Best Answer

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
\end{badidea}

You'll have a surprise. :)