5

Is there a way to allow a new item inside an underline command? In my real work, I have a custom command generating the "item" with its text, so moving the \underline to the other side of the \item isn't an option. Here's a MWE:

\documentclass{article}
\begin{document}
\begin{itemize}
\underline{\item abcd}
\end{itemize}
\end{document}

The error message I get is "Something's wrong - perhaps a missing \item".

Edit: Here's a less minimal working example:

\documentclass{article}
\usepackage{lipsum}

\newcommand{\course}[3]{\item[MATH #1] \emph{#2}\newline{#3}}

\newcommand{\sixten}{\course{610}{A Course Title}{A long description \lipsum[5]}}
\newcommand{\sixtwenty}{\course{620}{Another Course Title}{A shorter description}}

\begin{document}

{\large Course Offerings:}

Fall 2016
\begin{description}
\sixten
\sixtwenty
\end{description}

Spring 2017
\begin{description}
\underline{\sixten}
\sixtwenty
\end{description}
\end{document}
13
  • I'd like to underline both. In my main document I'm using the description environment, but I figured itemize would be easier to generalize for others. – dejongbrent Dec 11 '15 at 0:26
  • It matters if you are using description because then you are probably passing \item an argument. Is that right? That's rather different from the \item case in \itemize. The point is, you can't make this work just as it is, so something has to give and it matters what can give, for that reason. – cfr Dec 11 '15 at 0:43
  • 2
    @cfr: I don't have whatever in mind this time. Actually I think this would be very bad typography – a part of our activity here is devoted to some sort of typotherapy ;o) – Bernard Dec 11 '15 at 0:55
  • 1
    @Bernard I agree. However, this smells like a requirement to me. How many LaTeX users use underlining voluntarily? Maybe I'm wrong, but I would have thought not very many. I always forget that \underline is even a standard command. EDIT: Then again... – cfr Dec 11 '15 at 0:58
  • 1
    More seriously, a vertical bar between the bullet and the body of the item would be a simple highlighting. The framed package allows to do that, even in case of page breaks, if I remember well. – Bernard Dec 11 '15 at 1:14
10

I propose this, with the help of the framed package, slightly tweaking the leftbar environment. If you don't want grey lines, it's easy to remove the colour. Using xparse, I define a \course command, with $3$ mandatory arguments. A star version adds a left bar that can beak across pages:

\documentclass{article}
\usepackage{lipsum}
\usepackage{framed, xcolor, xparse}

\renewenvironment{leftbar}{%
  \def\FrameCommand{{\hspace{20pt}\color{lightgray}\vrule width 2pt}\hspace{-18pt}}%
  \MakeFramed {\advance\hsize-2\width \FrameRestore}}%
 {\endMakeFramed}

\DeclareDocumentCommand\course{ s m m +m }{%
\item[MATH #2] \emph{#3}\newline{%
\IfBooleanTF{#1}{\setlength\OuterFrameSep{-\topsep} \begin{leftbar}#4\end{leftbar}}%
{#4}}%
}

\newcommand{\sixtwenty}{\course{620}{Another Course Title}{A shorter description}}

\begin{document}

{\large Course Offerings:}\bigskip

Fall 2016
\begin{description}
\course*{610}{A Course Title}{A long description \lipsum[5]}
\sixtwenty
\end{description}

Spring 2017
\begin{description}

\course{610}{A Course Title}{A long description \lipsum[5]}
\sixtwenty
\end{description}

\end{document} 

enter image description here

3
  • You need a paragraph break after the Course Offerings thing. (I know it is in the original, but there's no need to repeat it, I guess.) – cfr Dec 11 '15 at 1:49
  • The idea was to have it only be highlighted sometimes, not always. I want to be able to highlight the courses I'm thinking of taking in a future semester, then perhaps return to the document next year, change my mind, and easily change which are highlighted. I do prefer the leftbar over a box, though, and the light grey looks nice. – dejongbrent Dec 11 '15 at 1:52
  • @dejongbrent: I've redefined the \course command with xparse, allowing for a star version that displays the left bar. – Bernard Dec 11 '15 at 23:40
6

Here's a way of drawing the left bar suggested by Bernard when a star is appended to the command. It requires defining new courses with \newcourse rather than \newcommand, but it involves less typing as an added bonus.

  • \newcourse{\newcommandname}{<number>}{<title>}{<description>} sets up a new course. It will create \newcommandname which will create the regular print of the course and \newcommandname* which will add a left bar in the margin.

  • \setlength\courselinewidth{<width>} controls the width of the line.

  • The colour and opacity of the line can be set with \coursehighlight[<colour>]{<% opacity>}. The default colour is black. Below, I show how to set the opacity and colour in a couple of ways, including one which is there just in case you get a colour printer and fancy a change.

Note that this will not work in the case that a page break occurs during the highlighted course, and no attempt is made to prevent or detect this.

Here are three examples:

black line

grey and blue lines

Code:

\documentclass{article}
\usepackage{xparse,tikz,tikzpagenodes}
\usetikzlibrary{tikzmark,calc}
\usepackage{lipsum}
\newcounter{hcourse}
\setcounter{hcourse}{0}
\newcommand{\course}[3]{\item[MATH #1] \emph{#2}\par #3}
\NewDocumentCommand\newcourse { m m m +m }{%
  \NewDocumentCommand #1 { s }{%
    \IfBooleanTF ##1 {%
      \stepcounter{hcourse}%
      \course{\tikzmark{\thehcourse a}#2}{#3}{#4}\tikzmark{\thehcourse b}%
      \tikz[remember picture, overlay]{%
        \foreach \i in {a,b} \coordinate (\thehcourse \i) at ({pic cs:\thehcourse \i});
        \draw [course highlight, line width=\courselinewidth] ($(\thehcourse a -| current page text area.west) + (-1em,\baselineskip)$) coordinate (\thehcourse c) -- ($(\thehcourse b -| \thehcourse c)$);
      }%
    }{%
      \course{#2}{#3}{#4}%
    }%
  }%
}
\NewDocumentCommand\coursehighlight { O {black} m }{%
  \colorlet{course highlight}{#1!#2}}
\newlength\courselinewidth
\setlength\courselinewidth{1pt}
\coursehighlight{75}
\newcourse{\sixten}{610}{A Course Title}{A long description \lipsum[5] Final words.}
\newcourse{\sixtwenty}{620}{Another Course Title}{A shorter description}

\begin{document}

\title{Course Offerings}
\author{}\date{}
\maketitle

\subsubsection*{Fall 2016}
\begin{description}
  \sixten
  \sixtwenty
\end{description}

\subsubsection*{Spring 2017}
\begin{description}
  \sixten*
  \sixtwenty
\end{description}

\coursehighlight[gray]{50}
\setlength\courselinewidth{1mm}
\begin{description}
  \sixten
  \sixtwenty*
\end{description}

In case you get a colour printer\dots
\coursehighlight[blue]{80}
\begin{description}
  \sixten*
  \sixtwenty
\end{description}
\end{document}

Original Answer (Corrected)

Here's a way of boxing the course when a star is appended to the command. It requires defining new courses with \newcourse rather than \newcommand, but it involves less typing as an added bonus.

\documentclass{article}
\usepackage{xparse,tikz,tikzpagenodes}
\usetikzlibrary{tikzmark,calc}
\usepackage{lipsum}
\newcounter{hcourse}
\setcounter{hcourse}{0}
\newcommand{\course}[3]{\item[MATH #1] \emph{#2}\par #3}
\NewDocumentCommand\newcourse { m m m +m }{%
  \NewDocumentCommand #1 { s }{%
    \IfBooleanTF ##1 {%
      \stepcounter{hcourse}%
      \course{\tikzmark{\thehcourse a}#2}{#3}{#4}\tikzmark{\thehcourse b}%
      \tikz[remember picture, overlay]{%
        \foreach \i in {a,b} \coordinate (\thehcourse \i) at ({pic cs:\thehcourse \i});
        \draw ($(\thehcourse a -| current page text area.west) + (-1em,\baselineskip)$) rectangle ($(\thehcourse b -| current page text area.east) + (1em,-.5\baselineskip)$);
      }%
    }{%
      \course{#2}{#3}{#4}%
    }%
  }%
}
\newcourse{\sixten}{610}{A Course Title}{A long description \lipsum[5] Final words.}
\newcourse{\sixtwenty}{620}{Another Course Title}{A shorter description}
\begin{document}

\title{Course Offerings}
\author{}\date{}
\maketitle

\subsubsection*{Fall 2016}
\begin{description}
  \sixten
  \sixtwenty
\end{description}

\subsubsection*{Spring 2017}
\begin{description}
  \sixten*
  \sixtwenty
\end{description}
\end{document}

new courses

11
  • May be a little too much vertical padding at the bottom of the box? +1 for the star version – Bernard Dec 11 '15 at 1:54
  • @Bernard It isn't actually padding. It is because the last line of the lipsum just fills the line. Is there a good way to avoid the additional line being added? If I remove the vertical space, it will cut through the last line of text in the majority of cases. – cfr Dec 11 '15 at 2:01
  • What part of the code senses whether or not there is a star? Is it the { s }? And what does { m m m +m } do? Edit: Is there something I should read to understand all this? – dejongbrent Dec 11 '15 at 2:10
  • 1
    @Bernard texdoc xparse explains it. The s is a star. The m m m +m are 4 mandatory arguments: short, short, short, long. The \IfBooleanTF is testing whether there is a star or not when the command is called. – cfr Dec 11 '15 at 2:16
  • 1
    @dejongbrent Indeed. I've never used that package. I used what I was familiar with. Bernard's solution is better. Well, the star thing is quite nice, I think. But you can combine that with Bernard's solution by just substituting his left bar code in for mine. And you don't need the counter in that case, either. – cfr Dec 11 '15 at 3:09
2

Thanks to Bernard, I have the following solution:

\documentclass{article}
\usepackage{lipsum,framed}
\newcommand{\course}[3]{\item[MATH #1] \emph{#2}\newline{#3}}
\newcommand{\sixten}{\course{310}{A Course Title}{A long description \lipsum[5]}}
\newcommand{\sixtwenty}{\course{320}{Another Course Title}{A shorter description}}
\begin{document}
{\Large Course Offerings:}

Fall 2016
\begin{description}
\sixten
\sixtwenty
\end{description}
Spring 2017
\begin{description}
\begin{leftbar}
\sixten
\end{leftbar}
\sixtwenty
\end{description}
\end{document}

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.