Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using \printexercise with a macro breaks the index in the .aux file #54

Closed
N-Coder opened this issue Nov 4, 2019 · 4 comments
Closed

Comments

@N-Coder
Copy link
Contributor

N-Coder commented Nov 4, 2019

I'm currently working on migrating a collection of exercises to XSIM. Thanks for this cool package and its large number of features!

In my case, all exercises are collected in a separate "database" tex file and then only some of them are used in separate tex files, one for each exercise sheet. The exercises are selected based on their title string, which was implemented using \csname:

% master.tex
\newcommand{\defex}[2]{%
  \expandafter\newcommand\csname exercise#1\endcsname{#2}%
}

\newcommand{\useex}[1]{ %
  \section{#1} %
  \csname exercise#1\endcsname
}

% database.tex
\defex{The $P=NP$ Problem}{Prove that $P=NP$.}

% sheet1.tex
\useex{The $P=NP$ Problem}

I tried to mimic this approach with XSIM by defining a new property title, and then finding the right exercises using \GetExerciseIdForProperty and printing them via \printexercise:

\DeclareExerciseProperty!{title}
\newcommand{\printexercisewithtitle}[1]{%
	\edef\foundid{\GetExerciseIdForProperty{title}{#1}}%
	\ifthenelse{\equal{\foundid}{}}{%
		No exercise with title ''#1'' found!\\
	}{%
		\printexercise{exercise}{\foundid}%
	}%
}

A mwe can be found here.
Unfortunately, the indirection via \GetExerciseIdForProperty doesn't work with \printexercise, as for all exercises included using this macro, the counter is not incremented (all show up as Exercise 1) and not all properties are accessible (e.g. the fourth one doesn't show a subtitle):
test1.pdf
Only the second one, which is included by directly passing its ID 2 to \printexercise shows up correctly. The cause of this can be found in the aux-file, where e.g. the values for counter are recorded as follows:

\XSIM {counter}{exercise-\foundid =={1}||exercise-2=={1}}

So this is probably caused by some problems with expansion of the macro. Unfortunately, throwing in \XSIMexpandcode in various places didn't help. I was only able to partialy fix this problem by introducing a further level of indirection with the following macros (see also the linked mwe):

\newcommand{\printexercisewithtitle}[1]{%
  \printexercisewithtitleproxy
    {#1}
    {\GetExerciseIdForProperty{title}{#1}}%
}
\newcommand{\printexercisewithtitleproxy}[2]{%
  \ifthenelse{\equal{#2}{}}{%
    No exercise with title "#1" found!\\
  }{%
    \printexercise{exercise}{#2}%
  }%
}

Now at least the counter is displayed correctly, even though it's recorded keys are still wrong (but at least they are unique):

\XSIM {counter}{exercise-\GetExerciseIdForProperty {title}{Erste}=={1}||exercise-\GetExerciseIdForProperty {title}{W\"eird ßt$u$ff}=={3}||exercise-\GetExerciseIdForProperty {title}{Vierte}=={4}||exercise-\GetExerciseIdForProperty {title}{Letzte}=={5}||exercise-2=={1}}

The (sub-)titles to be displayed in the heading are still broken:
test2.pdf
They seem to be recorded correctly, but they are probably looked up using exercise-\GetExerciseIdForProperty {title}{Erste} as key instead of their actual ID.

\XSIM {subtitle}{exercise-2=={Zweite}||exercise-4=={Die Vierte}}
\XSIM {title}{exercise-1=={Erste}||exercise-3=={W\"eird ßt$u$ff}||exercise-4=={Vierte}||exercise-5=={Letzte}||exercise-6=={Letzte}}

My question is now how to define \printexercisewithtitle properly to ensure the right amount of enpasion for the looked-up IDs.

@cgnieder
Copy link
Owner

cgnieder commented Nov 4, 2019

Please, can you reduce this into one compilable file (maybe with a filecontents environment for a necessary second file) and lets say maximum three exercises, that I can use reproduce the issue in one go?

@N-Coder
Copy link
Contributor Author

N-Coder commented Nov 5, 2019

There is one: test1.tex in the linked gist is independently compileable, no need for additional files or filecontents. The other files in the gist are just my attempt of solving the issue in test2.tex and the .aux files generated when compiling the respective tex files. All the code listings above are just excerpts from the files in the gist.

If you need an example that is really as minimal as it can get, use this one and ignore everything said above 😉

\documentclass{article}

\usepackage{xsim}

\DeclareExerciseProperty!{title}
\newcommand{\printexercisewithtitle}[1]{%
	\edef\foundid{\GetExerciseIdForProperty{title}{#1}}%
	\printexercise{exercise}{\foundid}%
}

\DeclareExerciseCollection{foo}

\begin{document}

\collectexercises{foo}
\begin{exercise}[title={Erste},subtitle={Die Erste}]
The first exercise is added to the collection `foo'.
\end{exercise}
\begin{exercise}[title={Zweite},subtitle={Die Zweite}]
The second exercise is also added to the collection `foo'.
\end{exercise}
\collectexercisesstop{foo}

\printexercisewithtitle{Erste}
\printexercisewithtitle{Zweite}

\printexercise{exercise}{1}
\printexercise{exercise}{2}

\end{document}

The output looks like this:
image
I would expect to see identical exercises 1 and 2 twice, but the first ones are not numbered correctly and don't have a title.

@N-Coder
Copy link
Contributor Author

N-Coder commented Nov 8, 2019

The issue seems to be similar to what you explained here on stackexchange.
My understanding was that only the parameter would need expansion (as in \printexercise{exercise}{\XSIMexpandcode{\foundid}}), but actually the whole printexercise thing needs expansion for some reason: \XSIMexpandcode{\printexercise{exercise}{\exid}}.
Maybe putting the code with the correct order of expansion from the stackexchange question as example right next to \printexercise in the XSIM documentation would also prevent others from running into this problem.

@cgnieder
Copy link
Owner

You are right, this is the same issue. I'll add \xprintexercise as a shortcut for \XSIMexpandcode{\printexercise{}{}} to the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants