2

This question is closely related to Command for switching exercise styles.

In the following problem the following code was deviced

\newcommand{\Problem}{
    \refstepcounter{problem}
    \vspace{3.25ex plus 1ex minus .2ex}\par
    \noindent\makebox[0pt][r]{\protect\fbox{\UNIproblemnumber}\qquad}%
    \par\vskip-\baselineskip\vskip-\parskip
}

Which removes the vertical spacing after the makebox command. Something like

\Problem

\lipsum[75]

Produces

enter image description here

Which is great. The problem arises when an enumerate environment directly follows

enter image description here

A way to remedy this is to insert \vspace{-\topsep} manually between \Problem and the enumerate environment. However, using this globally messes us the spacing between \Problem and normal paragraphs.

  • Question: Is there a way to reduce the spacing between \Problem and enumerate without effecting the spacing between \Problem and normal paragraphs?

Code

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

\newcounter{problem}
\newcommand{\UNIproblemnumberformat}{\bfseries\color{blue}}
\newcommand{\UNIproblemnumber}{\UNIproblemnumberformat\arabic{problem}}

\newcommand{\Problem}{
    \refstepcounter{problem}
    \vspace{3.25ex plus 1ex minus .2ex}\par
    \noindent\makebox[0pt][r]{\protect\fbox{\UNIproblemnumber}\qquad}%
    \par\vskip-\baselineskip\vskip-\parskip
}

\setlength\parindent{0pt}

\begin{document}

\Problem 

\begin{enumerate}
    \item 
\end{enumerate}

\Problem

\lipsum[75]

\begin{enumerate}
    \item Hi
\end{enumerate}

\lipsum[66]

\end{document}
2

This is best solved with an environment:

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

\usepackage{lipsum}

\newcommand{\UNIproblemnumberformat}{\bfseries\color{blue}}

\newlist{Problemenv}{enumerate}{1}
\setlist[Problemenv]{
  labelwidth=3em,
  align=left,
  label=\fbox{\UNIproblemnumberformat\arabic*},
  resume
}
\newenvironment{Problem}{\Problemenv\item}{\endProblemenv}
\newenvironment{penumerate}{\enumerate[leftmargin=*]}{\endenumerate}

\begin{document}

\begin{Problem}
\begin{penumerate}
    \item \lipsum[3][1-2]
    \item \lipsum[3][3-5]
\end{penumerate}
\end{Problem}

\begin{Problem}
\lipsum[75]

\begin{enumerate}
    \item Hi
\end{enumerate}

\lipsum[66]
\end{Problem}

\end{document}

enter image description here

If you want the toggle for the format, here is how.

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{changepage}

\usepackage{lipsum}

\newtoggle{inline}
\settoggle{inline}{true}
\newcommand{\UNIproblemnumberformat}{\bfseries\color{blue}}

\newlist{Problemenv}{enumerate}{1}
\setlist[Problemenv]{
  labelwidth=\dimexpr3em\relax,
  align=left,
  label=\fbox{\UNIproblemnumberformat\arabic*},
  resume
}
\newenvironment{Problem}
 {%
  \setlength{\parindent}{0pt}%
  \iftoggle{inline}
   {\Problemenv\item}
   {%
    \adjustwidth{\dimexpr3em-\labelsep\relax}{0pt}%
    \refstepcounter{Problemenvi}\subsection*{Problem \theProblemenvi}%
   }%
 }
 {\iftoggle{inline}{\endProblemenv}{\endadjustwidth}}
\newenvironment{penumerate}{\enumerate[leftmargin=*]}{\endenumerate}

\begin{document}

\begin{Problem}
\begin{penumerate}
    \item \lipsum[3][1-2]
    \item \lipsum[3][3-5]
\end{penumerate}
\end{Problem}

\begin{Problem}
\lipsum[75]

\begin{enumerate}
    \item Hi
\end{enumerate}

\lipsum[66]
\end{Problem}

\togglefalse{inline}

\begin{Problem}
\begin{penumerate}
    \item \lipsum[3][1-2]
    \item \lipsum[3][3-5]
\end{penumerate}
\end{Problem}

\begin{Problem}
\lipsum[75]

\begin{enumerate}
    \item Hi
\end{enumerate}

\lipsum[66]
\end{Problem}

\end{document}

enter image description here

  • You beat me by a few seconds! – NVaughan Nov 20 '19 at 14:26
  • How would I use this in combination with tex.stackexchange.com/questions/517121/…? – N3buchadnezzar Nov 20 '19 at 14:40
  • @N3buchadnezzar You don't. Using a command is wrong to begin with. – egreg Nov 20 '19 at 15:49
  • @egreg How would I do it with an environment then? – N3buchadnezzar Nov 20 '19 at 16:16
  • 1
    @N3buchadnezzar Added the requested version. – egreg Nov 20 '19 at 18:15
0

I would do the following. Since you're already using enumitem you can create a new list as follows:

\newlist{myenumerate}{enumerate}{1}
\setlist[myenumerate,1]{label=\arabic*., topsep=0pt}

The key, topsep=0pt, is what eliminates the undersired space.

So whenever you need enumerated lists after a problem, just call it thus:

\begin{myenumerate}
  \item
\end{myenumerate}

The whole code is:

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

\newcounter{problem}
\newcommand{\UNIproblemnumberformat}{\bfseries\color{blue}}
\newcommand{\UNIproblemnumber}{\UNIproblemnumberformat\arabic{problem}}

\newcommand{\Problem}{
    \refstepcounter{problem}
    \vspace{3.25ex plus 1ex minus .2ex}\par
    \noindent\makebox[0pt][r]{\protect\fbox{\UNIproblemnumber}\qquad}%
    \par\vskip-\baselineskip\vskip-\parskip
}

\newlist{myenumerate}{enumerate}{1}
\setlist[myenumerate,1]{label=\arabic*., topsep=0pt}

\setlength\parindent{0pt}

\begin{document}

\Problem

\begin{myenumerate}
    \item \lipsum[75]
\end{myenumerate}

\Problem

\lipsum[75]

\begin{myenumerate}
    \item Hi
\end{myenumerate}

\lipsum[66]

\end{document}

which produces:

enter image description here

You can also tweak the space left to the number by setting the keys leftmargin and itemindent.

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.