0

In a rather large document I'm writing, I've created a custom environment for exercises at the end of each chapter, in which each of the exercises takes the form of a 'theorem', i.e. I use a custom theorem style as the format of the exercises. It may be abuse, but it works.

However, the exercise numbers weren't aligning properly. Single digit exercises would be offset from the double digit exercises. This is why I included \hfill in the custom head spec entry of the \newtheoremstyle. Unfortunately, it turns out this gives me problems when including an image in an exercise; enter image description here

For reference, without the \hfill the alignment is as follows: enter image description here

My question is: How to get the exercises to align properly, both for single and double digit exercises, with and without images?

Here's an MWE, withouth the custom environment mentioned earlier, but with the custom theoremstyle. Thank you for taking the time to consider this.

\documentclass{report}
\usepackage[english]{babel}
\usepackage{amsthm}
\usepackage{float}
\usepackage{graphicx}

\newcounter{exenr}[chapter]
\setcounter{exenr}{8}
\newtheoremstyle{exestyle}
 {\topsep}
 {\topsep}
 {\small}
 {0pt}
 {\small}
 {.}
 {5pt plus 1pt minus 1pt}
 {\hfill\thmname{#1}\thmnumber{#2}}
\theoremstyle{exestyle}
 \newtheorem{exercise}[exenr]{}
\newcommand{\exe}[1]
 {\begin{exercise}{\parbox[t]{.95\textwidth{#1}}\end{exercise}}

\begin{document}
\exe{
This is an exercise that does not include an image.
The exercise number is in the proper place, 
and the text is properly aligned.
}
\exe{
This is an exercise that does contain an image.
The exercise number is misplaced, and the text misaligned.
\begin{figure}[H]\centering
\includegraphics{Circle.png}
\end{figure}
}
\end{document}
  • 1
    As you don't want the image to float, don't use the figure environment. Use center instead. – Bernard Dec 4 '16 at 21:01
  • 1
    what is the intention of the \parbox here? It is that, rather than the command definition that is causing the problem you are relying on .95\textwidth and the exercise number fitting on the line, and as Bernard said the figure environment is completely unwanted here. – David Carlisle Dec 4 '16 at 21:06
  • 1
    then something's wrong, the exercise setup is clearly a latex itemize-style list and the items should work without boxing (although that's a separate issue to the figure use) so I guess @Bernard should answer to that:-) – David Carlisle Dec 4 '16 at 22:31
1

I think the problem with alignment comes from the use of \hfill, and it does not have anything to do with the picture. As I understand your question you want something where each entry is numbered and indended some specific distance. Probably this is easier done using the list environment.

\documentclass{report}
\usepackage[english]{babel}
\usepackage{amsthm}
\usepackage{float}
\usepackage{graphicx}
%%
\newcounter{execnt}
\newcommand\exe[1]{%
  \begin{list}{\theexecnt.}{\refstepcounter{execnt}\setlength{\leftmargin}{4ex}}
    \item #1
  \end{list}}
\setcounter{execnt}{8}
%%
\begin{document}
\noindent Text text
\exe{\label{ThisExe} This is an exercise that does not include an image.
  The exercise number is in the proper place, 
  and the text is properly aligned.
}
\exe{In Exe~\ref{ThisExe} there is no picture. 
  This is an exercise that does contain an image.
  The exercise number is misplaced, and the text misaligned.
  \begin{center}
    \includegraphics[scale=0.25]{example-image-a}
  \end{center}
}
\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.