[Tex/LaTex] Exercise Package: Align Exercise Header, Exercise, Answer Header and Answer

exerciseshorizontal alignment

I am typesetting exercises with the exercise package. I want to have my custom exercise header, my custom answer header as well as the exercise and answer body all left aligned. It works well for the answer environment, but not really for the exercise part.

\documentclass[a4paper]{article}
\usepackage{exercise}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{amsmath}
\usepackage{amsthm} 
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[left=2cm,right=2.5cm,top=2.5cm,bottom=2cm]{geometry}
\numberwithin{equation}{Exercise}


\renewcommand{\ExerciseHeader}{\par\noindent\textsf{\textbf{
        Aufgabe \ExerciseHeaderNB} (\ExerciseHeaderTitle)
}\smallskip\newline}
%\ExerciseHeaderDifficulty
\renewcommand{\AnswerHeader}{\flushright \textbf{\textsf{Lösung
            \ExerciseHeaderNB}         }\ExerciseHeaderOrigin\smallskip\newline}

\renewcommand{\ExerciseHeaderTitle}{\ExerciseTitle}
\setlength{\ExerciseSkipBefore}{0\baselineskip}

\begin{document}

\begin{Exercise}[label = wdsw, title = Widerstandswürfel , difficulty =      2]
Gegeben ist ein Würfel, wobei jede der Kanten einen Widerstand von $R = 1~\mathrm{\Omega}$ hat.\\
Wie groß ist der Widerstand entlang einer Raumdiagonale?
\end{Exercise}
\begin{Answer}[ref=wdsw]
Wir wollen den Widerstand zwischen den Punkten $X$ und $Y$ bestimmen,   also entlang der Raumdiagonale (siehe Abb. \ref{fig:wdsws1}). Weil die Raumdiagonale eine Symmetrieachse ist, sollte das Problem symmetrisch sein, und deswegen eine recht einfache Lösung haben.\\
\end{Answer}

Best Answer

There are two parts to this answer:

  • After redefining the macros \ExerciseHeader and \AnswerHeader, the indentation of the the headlines equals the usual indentation of the text. You can set this to zero with:

    \setlength\parindent{0pt}
    
  • In the code of the exercise package, there is a hard-coded \hspace{.66em} at the start of the Exercise body. To neutralize it, you can redefine \AtBeginExercise:

    \renewcommand{\AtBeginExercise}{\hspace{-0.66em}}
    

So your full header section on the style of the Exercise and Answer could look like this:

\setlength\parindent{0pt}

\renewcommand{\DifficultyMarker}{}
\renewcommand{\ExerciseName}{Aufgabe}
\renewcommand{\AnswerName}{Lösung}

\renewcommand{\ExerciseHeader}{\large\textbf{\ExerciseName~\ExerciseHeaderNB} (\ExerciseTitle)\smallskip\newline}
\renewcommand{\AtBeginExercise}{\hspace{-0.66em}}

\renewcommand{\AnswerHeader}{\large\textbf{\AnswerName~\ExerciseHeaderNB}\smallskip\newline}

\setlength\AnswerSkipBefore{1em}

enter image description here


Update: You might be right, there is still too much space before the exercise title, and I'm not one hundred percent sure about the space in front of the exercise body. So with changing "Aufgabe" to "Hausaufgabe" and "Gegeben ist ein Würfel" to "Es ist ein Würfel gegeben" (to have straigth lines at the left to compare indentation more easily), I find this configuration satisfying to my eye:

\setlength\parindent{0pt}

\renewcommand{\DifficultyMarker}{}
\renewcommand{\ExerciseName}{Hausaufgabe}
\renewcommand{\AnswerName}{Lösung}

\newcommand{\AtBeginExerciseHeader}{\hspace{-0.2pt}}
\renewcommand{\ExerciseHeader}{\AtBeginExerciseHeader\large\textbf{\ExerciseName~\ExerciseHeaderNB} (\ExerciseTitle)\newline}
\renewcommand{\AtBeginExercise}{\hspace{-8.7pt}}

\renewcommand{\AnswerHeader}{\large\textbf{\AnswerName~\ExerciseHeaderNB}\smallskip\newline}

\setlength\AnswerSkipBefore{1em}

enter image description here

Related Question