I am writing a textbook in Lyx that includes (a) exercises throughout the text of the chapters and (b) additional exercises at the end of each chapter. The end-of-chapter exercises will be formatted as numbered lists like the ones found in regular textbooks. In order not to lose the connection between each exercise and its solution, I want to write the solution to both types of exercises right after the corresponding exercise. However, I want the solutions of (a) and (b) to be printed at the end of the book.
xsim
seems to be the most up-to-date Latex package for what I want. I plan to use the default
layout for exercises intermingled with the text of each chapter. For the end-of-chapter exercises, Clemens, the author of xsim
, advised me to define a template
that mimics the enumerate
environment:
\documentclass{book}
\usepackage{xsim}
\usepackage{kantlipsum}% dummy text
\DeclareExerciseType{xrcs}{
exercise-env = xrcs ,
solution-env = sltn ,
exercise-name = \XSIMtranslate{exercise} ,
exercises-name = \XSIMtranslate{exercises} ,
solution-name = \XSIMtranslate{solution} ,
solutions-name = \XSIMtranslate{solutions} ,
exercise-template = enumerate ,
solution-template = enumerate ,
% counter = exercise % if a shared counter is wanted
}
\DeclareExerciseEnvironmentTemplate{enumerate}
{\begin{itemize}\item[\GetExerciseProperty{counter}.]}
{\end{itemize}}
\begin{document}
\chapter{My chapter}
\section{Heading One}
\kant[1]
\begin{exercise}
First exercise
\end{exercise}
\section{Heading Two}
\kant[2]
\section{Exercises}
\begin{xrcs}
Exercise One
\end{xrcs}
\begin{xrcs}
Exercise Two
\end{xrcs}
\begin{xrcs}
Exercise Three
\end{xrcs}
\end{document}
I would like to know the following:
- Is
xsim
supported by or at least compatible with Lyx? - Will I be able to write and see formulas within the environments of this package in the usual Lyx UI way?
- Are there other better alternative solutions for what I need?
- How do I install the required packages in Lyx?
Many thanks,
Rodolfo
First edit
Many thanks for the detailed answer!
Everything worked as expected in the LyX GUI. But I had problems when trying to compile.
I checked TexLife and the xsim package was in the list. However, it was reported as missing while trying to compile. I installed the package and this problem was solved.
Still I can't compile: I will make toy examples to report the error messages for each.
First toy example: Including only the xsimExercises and sximSolutions I get the following errors on compilation: TO BE CONTINUED.
From the code provided by Clemens, I plan to copy only the following in the Preamble:
\usepackage{xsim}
\DeclareExerciseType{xrcs}{
exercise-env = xrcs ,
solution-env = sltn ,
exercise-name = \XSIMtranslate{exercise} ,
exercises-name = \XSIMtranslate{exercises} ,
solution-name = \XSIMtranslate{solution} ,
solutions-name = \XSIMtranslate{solutions} ,
exercise-template = enumerate ,
solution-template = enumerate ,
% counter = exercise % if a shared counter is wanted
}
\DeclareExerciseEnvironmentTemplate{enumerate}
{\begin{itemize}\item[\GetExerciseProperty{counter}.]}
{\end{itemize}}
I guess that the rest corresponds to the document he created. Any mistakes?
In the Style xsimExercise that you wrote, I cannot see an instruction that connects with the command exercise
of the xsim package to begin an exercise. All that I see is LatexName xrcs
Doing small changes, to the exercise environments I included the following:
Counter xsimSolution # adaptation
Within section
End
Counter solution # adaptation
Within section
End
Style xsimSolution # adaptation
Category Reasoning
Margin First_Dynamic
LatexType Environment
LatexName sltn # adaptation
NextNoIndent 1
ResetArgs 1
Argument 1
LabelString "(string)"
Decoration Minimalistic
EndArgument
# AddToToc Exercises # adaptation
# IsTocCaption 1 # adaptation
LabelSep xx
ParIndent MMM
ParSkip 0.4
ItemSep 0.2
TopSep 0.7
BottomSep 0.7
ParSep 0.3
Align Block
AlignPossible Left
LabelType Static
LabelCounter xsimSolution # adaptation
LabelString "xsimSolution \thexsimSolution" # adaptation
Font
Shape Up
Size Normal
EndFont
LabelFont
Shape Up
Series Bold
EndFont
RefPrefix sltn # adaptation
Requires xsim # idem
End
Style Solution
Category Reasoning
Margin First_Dynamic
LatexType Environment
LatexName solution # adaptation
ParagraphGroup 1
NextNoIndent 1
ResetArgs 1
Argument 1
LabelString "(string)"
Decoration Minimalistic
EndArgument
# AddToToc Exercises # adaptation
# IsTocCaption 1 # adaptation
LabelSep xx
ParIndent MMM
ParSkip 0.4
ItemSep 0.2
TopSep 0.7
BottomSep 0.7
ParSep 0.3
Align Block
AlignPossible Left
LabelType Static
LabelCounter solution # adaptation
LabelString "solution \thesolution" # adaptation
Font
Shape Up
Size Normal
EndFont
LabelFont
Shape Up
Series Bold
EndFont
RefPrefix sol # adaptation
End
Best Answer
What you need is to define these new environments to your LyX document.
You have already done it with the above settings written in your preamble, but LyX doesn't know anything about it yet.
In LyX, go to Document->Settings->Local Layout and inside the text box enter the following:
Click Validate and apply. Now you should see two new environments in the top left combo box:
A brief explanation about this code block:
LyX allows the user to define some new environments and commands to the editor, by means of customization.
The above code block is in fact a layout file that makes such changes.
Format 60
is the format number of the layout file. The format for the present version of LYX is format 60.Counter xsimExercise
sets a counter for this environment. Add +1 inside sections. If you want both of the environmentsexercise
andxrcs
to share the same counter, change this toThen we define the new environments and the way they look inside the editor with
Take a loot at section 5.3.7 in the manual to better understand those commands.
Then add the code preamble you wrote in the question to the preamble in LyX (Document->Settings->Preamble).
And you are ready to go.
One more thing:
After you are all set with the customization process, in order to make it more user-wide, save the above code block I wrote as a module file (with a
module
file extension) such asxsim.module
) and append it the following lines:Then move this module file to LyX's layouts directory in Mac OS
in windows (if I am not wrong)
and then reconfigure LyX (Tools->Reconfigure) and restart LyX.
Now you should be able to see the new module in the module list available to be used:
The thing is that the cycle
is very slow. So first make sure you are good with your module you are setting up in the Local Layout, and quickly validate it. Only then make the changes and update the module file in the layouts directory.