10

I want to create an environment star Exercise, but I can not. Please help me.

\documentclass{book} 
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{amsmath}
    \newtheorem{ex}{Exercise}[chapter]
    \begin{document}
    \chapter{First Chapter}
    \section{First section}
    \begin{ex}
    This is an Exercise
    \end{ex}

    \begin{ex}
    This is an Exercise
    \end{ex}

    \end{document}

If next is a difficult Exercise, how to define new environment Exercise star, for example \textbf{Exercise} $1.3^\text{*}$.

2
  • 1
    What do you want? Please explain your intention? Commented Oct 20, 2012 at 7:04
  • An alternative to the nice (accepted) answer: write the ex environment so that it accepts an optional * argument. That's easy with the xparse package. Commented Oct 20, 2012 at 13:30

3 Answers 3

16

Just modify the representation of the counter for difficult exercises:

\documentclass{book}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\newtheorem{ex}{Exercise}[chapter]
\newenvironment{ex*}
  {\renewcommand\theex{\thechapter.\arabic{ex}\rlap{$^*$}}\ex}
  {\endex}


\begin{document}
\chapter{First Chapter}
\section{First section}
\begin{ex}
This is an Exercise
\end{ex}

\begin{ex}
This is an Exercise
\end{ex}

\begin{ex*}
This is a difficult Exercise
\end{ex*}

\end{document}

enter image description here


If the asterisk is not desired in the references to this exercise, then the definition of ex* can be changed into

\makeatletter
\newenvironment{ex*}
  {\renewcommand\theex{\thechapter.\arabic{ex}\rlap{$^*$}}%
   \ex\edef\@currentlabel{\thechapter.\arabic{ex}}}
  {\endex}
\makeatother

so that the reference will be created without *.

7
  • I had a problem like this once. I can use it again. Thanks @egreg.
    – hpesoj626
    Commented Oct 20, 2012 at 7:29
  • @hpesoj626 There may be a problem if you want to \label the exercise and then refer to it: with this solution the * will be printed also by \ref. It can be changed, if needed: please, let me know
    – egreg
    Commented Oct 20, 2012 at 7:31
  • Yes I see the *. Can you update the answer to so that the asterisk is not included by \ref? I was supposed to ask in chat but connection is slow at the moment.
    – hpesoj626
    Commented Oct 20, 2012 at 7:50
  • @hpesoj626 I've put the addition you requested.
    – egreg
    Commented Oct 20, 2012 at 19:44
  • Thanks. What does the new line \ex\edef\@currentlabel{\thechapter.\arabic{ex}} actually do? I don't know what \edef does to \@currentlabel. Is there already a question here or material that explains these? Let me know if I have to ask this in a separate post or in chat. Thanks.
    – hpesoj626
    Commented Oct 20, 2012 at 23:20
3

The package Exercise can help here:

\documentclass{article}

\usepackage{exercise}

\begin{document}
\begin{Exercise}
    This is an exercise.
\end{Exercise}

\begin{Exercise}[difficulty=1]
    This is a difficult exercise.
\end{Exercise}
\end{document}

The result:

Example of exercise package

2

I have just found an answer

 \documentclass[12pt,openany,twoside]{book} 
\usepackage{fouriernc}
\usepackage[thmmarks,standard,thref]{ntheorem}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}

\theoremseparator{.}
\theorembodyfont{\upshape}
\newtheorem{ex}{Exercise}[chapter]
\theoremseparator{$^\text{*}$.}
\newtheorem{dex}[ex]{Exercise}

\usepackage{answers}
\Newassociation{sol}{Answer}{loigiaichung}
\Newassociation{sols}{ansF}{loigiaichung}         
\newcommand{\preansF}{}                               
\renewcommand{\ansFlabel}[1]{{\bfseries #1*.}}         
\renewcommand{\Answerlabel}[1]{\bfseries #1.}
\begin{document}
\chapter{First Chapter}
\Opensolutionfile{loigiaichung}[ex]
\section{First section}
\begin{ex}
This is an exercise
\begin{sol}
This is a solution
\end{sol}
\end{ex}

\begin{dex}
This is an exercise
\begin{sols}
This is a solution
\end{sols}
\end{dex}


\chapter{Second Chapter}
\Opensolutionfile{loigiaichung}[ex]
\section{Second  section}
\begin{ex}
This is an exercise
\begin{sol}
This is a solution
\end{sol}
\end{ex}

\begin{dex}
This is an exercise
\begin{sols}
This is a solution
\end{sols}
\end{dex}

\Closesolutionfile{loigiaichung}
\section{Hint, anserw}
\input{ex}

\end{document}
2
  • You shouldn't be using $^\text{*}$, which produces too high an asterisk: either $^{*}$ or simply *. However, the position of the asterisk will be wrong if you use the optional argument to dex: \begin{dex}[From \cite{xyz}] will have the asterisk after the attribution.
    – egreg
    Commented Oct 20, 2012 at 22:12
  • Despite the formatting discussion in comments, this answer appears to be significantly better than the accepted one? Commented May 23, 2022 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .