5

I'm creating some lecture notes for my class using the book class, it is structured by chapters and sections. In some of my sections I want to add homework (and in some, I don't). I've been accomplishing this with the amsthm package:

\newtheorem{homew}{Homework} 
\begin{homew} Solve for $x$
\begin{enumerate}
\item $1=x+2$
\item $1=x-2$
\end{homew}

However, I want this homework to be numbered by the section it is in, so I can call back to it. So for example I want my document to look like this:

Chapter 1

Section 1

Homework 1

Section 2

Section 3

Section 4

Homework 4

And so on.

My question is two-fold:

  1. How to accomplish this? The counter options when creating a newtheorem don't do it. I don't want the Homework to be numbered as Homework 4.1 (there's no Homework 4.2, so what's the point?), and giving it its own counter doesn't work since then I get Homework 2 in Section 4, which I don't want either.
  2. Is there a more elegant way of doing this? I'm really new to LaTeX so I appreciate any help.
3
  • 1
    what if you add \renewcommand*{\thehomew}{\arabic{section}} right after the definition of the homew environment?
    – pluton
    Dec 13, 2015 at 4:34
  • Hey, that works for me!, Thank you pluton. If anyone else wishes to help me answer question 2, I'd be grateful. It just seems like newtheorem isn't the best tool to add homework but I'm new to this and for now it will do. Dec 13, 2015 at 4:39
  • I think it is a good idea to use the amsthm package for this. There might be other ways but this one is fine.
    – pluton
    Dec 13, 2015 at 4:47

3 Answers 3

3

Here's a tcolorbox solution for a nicer output and using \thesection as counter:

\documentclass{article}

\usepackage{mathtools}
\usepackage{amsthm}

\usepackage[most]{tcolorbox}


\newtcbtheorem{homew}{Homework}{title={\tcbtitle \thesection},enhanced jigsaw,boxrule=0pt,coltitle=black,
  sharp corners,colback=white,colbacktitle=white!40!yellow}{homew}

\begin{document}

\section{First}

\begin{homew}{Solve for $x$}{first}
\begin{enumerate}
\item $1=x+2$
\item $1=x-2$
\end{enumerate}
\end{homew}

\section{Second}
\begin{homew}{Solve for $x$ and $y$}{second}
\begin{align}
  1 &= x+y \\
  1 &= x-y
\end{align}
\end{homew}

\section{Another section}    

\begin{homew}{Solve for $x$ and $y$ and $z$}{third}
\begin{align}
  1 &= x+y-z \\
  1 &= x-y-z \\
  1 &= x+y+z
\end{align}
\end{homew}



\end{document}

enter image description here

4
  • Thank you, that looks very pretty, I will definitely experiment with it. Dec 13, 2015 at 22:49
  • @TheFourthMan: You're welcome to use it!
    – user31729
    Dec 15, 2015 at 19:03
  • Is there a way so that the 'homew' environment absorbs enumerate? I find that more often than not I'm inputing both. Dec 16, 2015 at 7:47
  • @TheFourthMan: What do you mean by 'absorb'? It should provide you with enumerate automatically?
    – user31729
    Dec 16, 2015 at 18:51
3

This is probably the easiest way:

\documentclass{article}

\usepackage{amsmath, amsthm}

\newtheorem{homew}{Homework} 
\def\thehomew{\thesection}

\begin{document}
\section{Some Section}

\begin{homew} Solve for $x$
    \begin{enumerate}
        \item $1=x+2$
        \item $1=x-2$
    \end{enumerate}
\end{homew}

\section{Some Other Section}

\begin{homew} Solve for $y$
    \begin{enumerate}
        \item $1=y+2$
        \item $1=y-2$
    \end{enumerate}
\end{homew}

\section{New Section}

\section{Last Section}

\begin{homew} Solve for $z$
    \begin{enumerate}
        \item $1=z+2$
        \item $1=z-2$
    \end{enumerate}
\end{homew}

\setcounter{section}{99}
\section{Just Kidding}

\begin{homew} Solve for $w$
    \begin{enumerate}
        \item $1=w+2$
        \item $1=w-2$
    \end{enumerate}
\end{homew}

\end{document}

enter image description here

1
  • @Mico Oops, at first I thought op was asking for something else and I forgot to remove that line.
    – Francis
    Dec 13, 2015 at 7:20
2

Here's a solution that uses the \AtBeginEnvironment macro of the etoolbox package to modify the counter variable that's associated with the hw environment.

\documentclass{article}
\usepackage{ntheorem}     % Or: \usepackage{amsthm}
\newtheorem{hw}{Homework} % Set up "Homework" environment

\usepackage{etoolbox}
\AtBeginEnvironment{hw}{% % Modify "hw" counter variable
   \setcounter{hw}{\value{section}}
   \addtocounter{hw}{-1}}

\begin{document}
\section{A}
\begin{hw} abc \end{hw}
\section{B}
\section{C}
\begin{hw} def \end{hw}
\section{D}
\begin{hw} ghi \end{hw}
\end{document}
3
  • Yes, you're right. One of my bad postings -- apparently I've lost my sense of TeX considering the huge criticism during the last days/weeks :-(. Thanks for informing me.
    – user31729
    Dec 13, 2015 at 7:34
  • @ChristianHupfer - What "huge criticism"? :-(
    – Mico
    Dec 13, 2015 at 7:49
  • Too long for a comment and I should replace huge by constant, perhaps. Don't worry about that.
    – user31729
    Dec 13, 2015 at 7:55

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.