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'd like to write a LaTeX environment that automatically places a \marginpar next to the first line of text within. However, that first line of text might not be part of an ordinary paragraph; it can be something like a list item.

In order for the \marginpar to align with the first line of an ordinary paragraph, I have to use \leavevmode to ensure that the \marginpar is placed in the same line box as the text that follows it; otherwise it's positioned too high. But if what follows is a list, this just creates a blank line for the \marginpar to align with, and the list goes below it.

This example illustrates the problem:

\documentclass{article}
\usepackage{geometry}
\geometry{reversemarginpar}

\newenvironment{category}{
  \leavevmode\marginpar{\raggedleft Label}
}{}

\begin{document}
  \begin{category}
    Lorem ipsum dolor sit amet.
  \end{category}

  \begin{category}
    % Label is aligned with this line.
    \begin{itemize}
      \item Item 1
      \item Item 2
      \item Item 3
    \end{itemize}
  \end{category}
\end{document}

The second category's label is positioned next to the blank line where the commented-out text would appear if it were uncommented. I'd like it to appear next to the line that says "Item 1" instead.

I believe this happens because the itemize list begins with something (probably a \par) that terminates the current line box and begins a new one. If I could tell TeX to inject the \marginpar into the beginning of the next line box it creates, rather than creating one explicitly with \leavevmode, I think that would resolve the problem. But I don't know how to do that.

Is there a way to make this work?

Update: After further consideration, this doesn't necessarily need to be a \begin{category} environment. I'm actually using this in a custom document class that consists entirely of a sequence of categories, so it may make more sense to just define a \category command that's used like the \chapter command in other classes. The desired output is the same, though.

Update 2: Can I create something analogous to \llap in vertical mode? I tried \vbox to 0pt{\vss X}\nointerlineskip hoping that the X would overlap with the line of text that follows, but it doesn't. If I can create a line box that overlaps the material below it, I can use that to position the \marginpar so it aligns with the "real" line box that's created later.

share|improve this question
add comment

1 Answer

Instead of the standard \marginpar command, you could use \marginnote from the marginnote package, and change the nesting order of your environment and the itemize:

\documentclass{article}
\usepackage{geometry}
\usepackage{marginnote}
\geometry{reversemarginpar}

\newenvironment{category}{
  \marginnote{\raggedleft Label}
}{}

\begin{document}

\begin{category}
    Lorem ipsum dolor sit amet.
\end{category}

\begin{itemize}
  \item \begin{category} Item 1
  \item Item 2
  \item Item 3
  \end{category}
\end{itemize}

\end{document}

share|improve this answer
    
I'll take a look at the marginnote package, but I don't think the reversed nesting order you suggested will work well for me: I'd like to be able to put multiple paragraphs (and/or lists, etc.) within a single category. –  Wyzard May 8 '11 at 2:00
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.