GeneralColoring alternate list rows?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Coloring alternate list rows?

Post by tripwire45 »

This tasks is next on my "hit parade". I've got a Glossary that's written as a description list:

Code: Select all

\newpage
\section{Glossary}
\subsection*{A}
\begin{description}
\item [Admin] Person in charge.
\item [Agent] A mean dude program with shades in the Matrix.
\item [Anonymous] What you want to be when an Agent is around.
\end{description}

\subsection*{B}
\begin{description}
\item [Blacklist] The list you don't want to be on
\item [Blue Pill] "Why oh why, didn't I take the BLUE pill?

<snip>

\subsection*{Z}
\begin{description}
 \item[Zebra] Black and white thing that looks like a horse with stripes.
 \end{description}
I know you can color alternate rows in a table, but is there a way to color alternate items in a list? It would be a pain in the pinfeathers to try to turn this list into a series of tables, just to accomplish the effect.

Try as I might, I can't find any documentation to say this is possible. I can put individual lines in a colored box, but the coloring ends at the end of the text, so rows would be uneven, rather than a uniform length.

I'm probably "reaching" in asking this question, but I thought it was worth a shot. Thanks.

-Trip

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10360
Joined: Mon Mar 10, 2008 9:44 pm

Re: Coloring alternate list rows?

Post by Stefan Kottwitz »

Hi Trip,

I don't know a solution for that coloring of list items. What does not mean that there isn't one. ;)
If it's enough to help you to get the colorboxes to uniform length, let us know, that may be easily possible with \hphantom for instance.

Stefan
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Coloring alternate list rows?

Post by tripwire45 »

I think colorboxes with a uniform length would work, as long as the boxes could be made to extend beyond the length of a text sentence.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Coloring alternate list rows?

Post by gmedina »

tripwire45 wrote:...is there a way to color alternate items in a list?

Hmm... Interesting question. Just to be sure of exactly what you need, please take a look at the following example:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\usepackage{calc}

\newlength\mywid
\setlength\mywid{\textwidth-31pt}

\begin{document}

\begin{enumerate}
  \item[One] \colorbox{blue!30}{\parbox{\mywid}{First item}}
  \item[Two] \colorbox{green!90}{\parbox[t]{\mywid}{Second item including some text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text}}
  \item[Three] \colorbox{blue!30}{\parbox{\mywid}{Third item}}
\end{enumerate}

\end{document}
Is something like this what you want to obtain?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Coloring alternate list rows?

Post by tripwire45 »

Yeah, gmedina. That works fine. Thanks. :D
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Coloring alternate list rows?

Post by tripwire45 »

I spoke too soon. :(

For some reason, this causes the row length to vary from one row to the next. I'm attaching a png file to illustrate. Here's my code:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\usepackage{calc}

\newlength\mywid
\setlength\mywid{\textwidth-31pt}

\begin{document}

\newpage
\section{Glossary}

\subsection*{A}
\begin{description}
\item [Admin] \colorbox{webgreen!15}{\parbox{\mywid}{Responsible for overall maintenance of the computing environment.}}
\item [Agent] {\parbox{\mywid}{A Really, really bad self-aware program that carries a gun.}}
\item [Anonymous] \colorbox{webgreen!15}{\parbox{\mywid}{What you really want to be when an Agent is around.}}
\item [Approve] {\parbox{\mywid}{An acceptable risk.}}
\end{description}

\end{document}
Attachments
glossary.png
glossary.png (19.15 KiB) Viewed 6022 times
User avatar
tripwire45
Posts: 129
Joined: Thu Apr 10, 2008 4:35 am

Re: Coloring alternate list rows?

Post by tripwire45 »

Just to bring some resolution to this thread, I've decided to use tables instead of lists. Since I can now create tables without floats, it's a much easier solution. Thanks for all your comments.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Coloring alternate list rows?

Post by gmedina »

Your choice seems to be a sensible one. However, using my previous idea and the enumitem package you can get something like:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{enumitem}

\newlength\mywid
\setlength\mywid{\textwidth-65.22237pt}

\begin{document}

\newpage
\section{Glossary}

\subsection*{A}
\begin{enumerate}[align=left,leftmargin=*,widest=Anonymous]
  \item[Admin] \colorbox{green!15}{\parbox[t]{\mywid}{Responsible for overall maintenance of the computing environment.}}
  \item[Agent] A Really, really bad self-aware program that carries a gun.
  \item[Anonymous] \colorbox{green!15}{\parbox[t]{\mywid}{What you really want to be when an Agent is around.}}
  \item[Approve] An acceptable risk.
\end{enumerate}

\end{document}
This example and the previous one were only that, examples...; of course they admit improvements.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply