5

I'm trying to make an environment that prints highlighted notes, and I can't get the vertical alignment right. Here's a minimal example:

\documentclass{article}

\usepackage{xcolor}
\usepackage{calc}

\definecolor{shadecolor}{rgb}{1,1,0}
\makeatletter%
\newenvironment{note}{%
  \begin{lrbox}{\@tempboxa}\begin{minipage}[t]{\linewidth-7pt}%
      \setlength{\parskip}{8pt}%
      \textbf{Note.}}%
    {\end{minipage}\end{lrbox}%
  \colorbox{shadecolor}{\usebox{\@tempboxa}}%
}%
\makeatother%

\begin{document}

\begin{enumerate}
\item
  \begin{note}
    This alignment is what I want.

    The top lines up with the number. 
  \end{note}

\item
  \begin{note}
    But if there's a list in the note, the number instead matches up
    with the first item. 

    \begin{itemize}
    \item A
    \item B
    \end{itemize}
  \end{note}
\end{enumerate}

\end{document}

This produces the following:

example

I'd like the highlighted box to be top-aligned with the enumerate numbers to the left; the first one is fine, but the second one is using the first item of the itemize instead. What have I done wrong?

6

Subtle problem. You see that not only the box is displaced, but also the item number.

One should never say \item \colorbox{color}{text}, but always

\item \leavevmode\colorbox{color}{text}

because of funny interactions of color setting and \item.

\documentclass{article}

\usepackage{xcolor}
\usepackage{calc}

\definecolor{shadecolor}{rgb}{1,1,0}
\newsavebox{\notebox}
\newenvironment{note}
 {\leavevmode
  \begin{lrbox}{\notebox}
  \begin{minipage}[t]{\linewidth-2\fboxsep}%
  \setlength{\parskip}{8pt}%
  \textbf{Note.} \ignorespaces}
 {\end{minipage}
  \end{lrbox}%
  \colorbox{shadecolor}{\usebox{\notebox}}%
  }

\begin{document}

\begin{enumerate}
\item
  \begin{note}
    This alignment is what I want.

    The top lines up with the number. 
  \end{note}

\item
  \begin{note}
    But if there's a list in the note, the number instead matches up
    with the first item. 

    \begin{itemize}
    \item A
    \item B
    \end{itemize}
  \end{note}

\end{enumerate}

\end{document}

I'd avoid using \@tempboxa, preferring a new box register. I made also a couple of adjustments. I'd also avoid setting \parskip.

Basically, what happens is that processing \item is unfinished and the inner \item takes over.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.