Does Lyx support exercises throughout the text of a textbook whose solutions are written after each exercise but printed at the end of the textbook

book-designbookslyxpackages

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:

  1. Is xsim supported by or at least compatible with Lyx?
  2. Will I be able to write and see formulas within the environments of this package in the usual Lyx UI way?
  3. Are there other better alternative solutions for what I need?
  4. 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:

Format 60

Counter xsimExercise
    Within         section
End

Counter exercise
    Within         section
End

Style xsimExercise
    Category              Reasoning
    Margin                First_Dynamic
    LatexType             Environment
    LatexName             xrcs
    NextNoIndent          1
    ResetArgs             1
    Argument 1
        LabelString   "(string)"
        Decoration    Minimalistic
    EndArgument
    AddToToc              Exercises
    IsTocCaption          1
    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          xsimExercise
    LabelString           "xsimExercise \thexsimExercise"
    Font
      Shape               Up
      Size                Normal
    EndFont
    LabelFont
      Shape               Up
      Series              Bold
    EndFont
    RefPrefix             xrcs #label prefix when iserting a label
           Requires              xsim
End

Style Exercise
    Category              Reasoning
    Margin                First_Dynamic
    LatexType             Environment
    LatexName             exercise
    ParagraphGroup        1
    NextNoIndent          1
    ResetArgs             1
    Argument 1
        LabelString   "(string)"
        Decoration    Minimalistic
    EndArgument
    AddToToc              Exercises
    IsTocCaption          1
    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          exercise
    LabelString           "exercise \theexercise"
    Font
      Shape               Up
      Size                Normal
    EndFont
    LabelFont
      Shape               Up
      Series              Bold
    EndFont
    RefPrefix             exer
End

Click Validate and apply. Now you should see two new environments in the top left combo box:

enter image description here

enter image description here

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 environments exercise and xrcs to share the same counter, change this to

Counter xsimExercise
    Within         section
End

Counter exercise
    Within         xsimExercise
End

Then we define the new environments and the way they look inside the editor with

Style xsimExercise
  defintions
End

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 as xsim.module) and append it the following lines:

#\DeclareLyXModule{Exercise Environments}
#DescriptionBegin
#Defines exercise environments with the xsim package
#DescriptionEnd

<Code block goes here>

Then move this module file to LyX's layouts directory in Mac OS

/Applications/LyX.app/Contents/Resources/layouts/

in windows (if I am not wrong)

C:/Users/laba/AppData/Roaming/LyX2.3/layouts

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:

enter image description here

The thing is that the cycle

  1. change module commands
  2. reconfigure LyX
  3. restart LyX
  4. check that you are good with the new configuration
  5. make some changes again

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.