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 am trying to use \newenvironment, written by somebody else:

with this code:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}


\newenvironment{alignedDescription}[2][0pt]
  {\begin{list}{}%
    {\renewcommand\makelabel[1]{\textsf{\bf##1}\hfil}%
     \settowidth\labelwidth{\makelabel{#2}}%
     \setlength\leftmargin{\labelwidth+\labelsep + #1}}}%
  {\end{list}}

\begin{document}


\begin{alignedDescription}{A}{B}
\item A
\item B
\end{alignedDescription}

\end{document}

I do not understand how it works. But for sure it does not work properly! It shows an error.

Could you help me to understand how does this enviroment work?

I've googled, but without any help for my problem.

share|improve this question
1  
    
@Werner Good spot, I've updated my answer. –  Thruston Jan 27 at 20:18

1 Answer 1

up vote 8 down vote accepted
  1. You are missing \usepackage{calc} to allow you to use + in the environment definition.

  2. The environment wants one optional argument and one normal argument, and the first one should be a valid TeX length. The optional argument wants square brackets not braces.

  3. You need to give each \item an optional label.

  4. If you want the labels in bold sans, then use \textbf{} rather than \bf inside the \textsf{}, as the old commands like \bf ignore the enclosing font choice.

Try it like this:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{calc}

\newenvironment{alignedDescription}[2][0pt]
  {\begin{list}{}%
    {\renewcommand\makelabel[1]{\textsf{\textbf{##1}}\hfil}%
     \settowidth\labelwidth{\makelabel{#2}}%
     \setlength\leftmargin{\labelwidth+\labelsep + #1}}}%
  {\end{list}}

\begin{document}


\begin{alignedDescription}[5pt]{Longest label}

\item[Label] A first thing
\item[B] Another thing

\end{alignedDescription}

\end{document}

How does \newenvironment work?

To define a simple new environment with no arguments in LaTeX we write

\newenvironment{name}{...opening commands...}{...closing commands...}

If we want to pass (up to 9) arguments to the environment we write

\newenvironment{name}[n]{...opening stuff...}{...closing stuff...}

where n is a number from 1 to 9 and each argument is then available in ...opening stuff... as #1, #2, and so on. So an environment defined as

\newenvironment{foo}[3]{...opening...}{...closing...}

would be called as

\begin{foo}{a}{b}{c}
   ... foo contents ...
\end{foo}

As an added sophistication, LaTeX allows us to make the first argument optional. To do this we provide a default value, like this:

\newenvironment{name}[n][default_for_#1]{...opening...}{...closing...}

So an environment defined as

\newenvironment{bar}[3][3.1415]{...open...}{...close...}

could be called as

\begin{bar}{b}{c}
   ... contents of bar with #1=3.1415 ...
\end{bar}

or as

\begin{bar}[2.718]{b}{c}
   ... contents of bar with #1=2.718 ...
\end{bar}

Notice that the optional first argument must be in [brackets] if it is present.

What happened in the original problem was that the defined environment wanted 2 arguments and the first one was optional, but the OP gave two arguments both in {braces}; so TeX used the default value for the first argument, the first {brace} as the second argument and treated the second {brace} as part of the content of the environment. Since the environment was a list, the contents were supposed to start with an \item, which is why the OP got a "missing item" error.

I can recommend The Latex Companion as a reference for this sort of thing.

share|improve this answer
    
Thanks for answer, but I also want to understand this. You said:"The environment wants one optional argument and one normal argument, and the first one should be a valid TeX length. The optional argument wants square brackets not braces." But why it needs one argument if it written \newenvironment{alignedDescription}[2][0pt] (2 in square brackets)? Thank You. –  saldenisov Jan 27 at 20:20
    
I'll update the answer with an explanation and a pointer to a tutorial. –  Thruston Jan 28 at 13:46

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.