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

share|improve this question
add comment

3 Answers

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

share|improve this answer
add comment

The minipage is your friend

\documentclass{article}
\begin{document}
\hfill
\begin{minipage}{0.5\linewidth}
  Address \\
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...

share|improve this answer
add comment

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}
\newenvironment{head}%
{
\centering
\begin{flushright}    
\begin{minipage}{5cm}}%
{
\end{minipage}
\end{flushright}}

\begin{document}
\begin{head}
First line and yeah, the head is right aligned.\\
A second line to show the text inside is left aligned.
\end{head}
Well, here comes the rest of the letter.
\end{document} 
share|improve this answer
    
Great stuff, thanks a lot. –  Petter Jul 4 '11 at 16:32
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.