From 9467b011517db19bab4d1bba53fe67c442bd4305 Mon Sep 17 00:00:00 2001
From: Sebastian Kuhnert
Date: Wed, 19 Oct 2016 15:14:14 +0000
Subject: automatically label exercises using the filename; allow references to
 \input'ed exercises in other sheets

---
 exercisesheets.sty | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

(limited to 'exercisesheets.sty')

diff --git a/exercisesheets.sty b/exercisesheets.sty
index a99f0a8..b5a81c7 100644
--- a/exercisesheets.sty
+++ b/exercisesheets.sty
@@ -20,6 +20,9 @@
 
 \RequirePackage{amsmath}
 
+\RequirePackage{currfile}
+\RequirePackage{catchfile}
+
 % configuration keys
 
 \newif\ifexsh@solutions
@@ -438,16 +441,24 @@
   \ifstrequal{#1}{\begin}{\exsh@skipsheet@begin}{%
     \ifstrequal{#1}{\end}{\exsh@skipsheet@end}{%
       \ifstrequal{#1}{\label}{\exsh@skipsheet@label}{%
-        \ifstrequal{#1}{\setcounter}{\expandafter\exsh@skipsheet\setcounter}{%
-          \ifstrequal{#1}{\item}{\ifdef{\@enumctr}{\@hyper@itemtrue\refstepcounter{\@enumctr}}{}}{}%
-          \exsh@skipsheet}}}}%
+        \ifstrequal{#1}{\input}{\exsh@skipsheet@input}{%
+          \ifstrequal{#1}{\exsh@skipsheet@endfile}{\currfile@pop\exsh@skipsheet}{%
+            \ifstrequal{#1}{\setcounter}{\expandafter\exsh@skipsheet\setcounter}{%
+              \ifstrequal{#1}{\item}{\ifdef{\@enumctr}{\@hyper@itemtrue\refstepcounter{\@enumctr}}{}}{}%
+              \exsh@skipsheet}}}}}}%
 }
 \def\exsh@skipsheet@end#1{%
   \ifstrequal{#1}{sheet}{\end{sheet}}{\endgroup\exsh@skipsheet}%
 }
 \def\exsh@skipsheet@begin#1{%
   \begingroup
-  \ifstrequal{#1}{exercise}{\refstepcounter{section}}{}%
+  \ifstrequal{#1}{exercise}{\refstepcounter{section}%
+    % automatic label for the first exercise in every included file
+    \ifthenelse{\equal{\jobname}{\currfilebase}}{}{%
+      \ifcsdef{exsh@autolabel@\currfilebase}{}{%
+        \csdef{exsh@autolabel@\currfilebase}{}%
+        \label{ex:\currfilebase}}}%
+  }{}%
   \ifinlist{#1}{\exsh@enums}{%
     \exsh@skipsheet@beginenum
   }{%
@@ -474,6 +485,12 @@
 \def\exsh@skipsheet@label#1{%
   \label{#1}\exsh@skipsheet
 }
+\def\exsh@skipsheet@input#1{%
+  \CatchFileDef{\exsh@inputfilecontent}{#1}{}%
+  \currfile@push
+  \currfile@set{#1}%
+  \expandafter\exsh@skipsheet\exsh@inputfilecontent\exsh@skipsheet@endfile
+}
 
 \def\exsh@sheethead{%
   \pgfkeysgetvalue{/exsh/title}{\exsh@tmp}%
@@ -600,6 +617,11 @@
     \pgfkeysvalueof{/exsh/strings/exercise}~\arabic{section}\exsh@exname\exsh@firstline\expandonce\exsh@points}%
   \unskip\vskip\pgfkeysvalueof{/exsh/below exercise title skip}\relax
   \@afterindentfalse
+  % automatic label for the first exercise in every included file
+  \ifthenelse{\equal{\jobname}{\currfilebase}}{}{%
+    \ifcsdef{exsh@autolabel@\currfilebase}{}{%
+      \csdef{exsh@autolabel@\currfilebase}{}%
+      \label{ex:\currfilebase}}}%
   % Use \leavevmode to ensure proper distances above environments that start
   % with a new \par. Make sure that a \label does not mess up the layout.
   \@ifnextchar\label{\exsh@label}{\leavevmode\ignorespaces}%
-- 
cgit v1.2.3

