3

I can do the following:

\begin{samepage}
\begin{exercise}
Long text question or maybe a table and some stuff etc.
\end{exercise}
\end{samepage}

But I would prefer to have this automated (since I feel like this would be possible).

I have tried declaring a new exercise environment. Trying to follow the \tcolorbox example in the xsim package manual but this does not work.

\documentclass[]{article}
    \usepackage{xsim}
    \usepackage{lipsum}
    
    \DeclareExerciseEnvironmentTemplate{customex}
      {%
        \begin{samepage}
        \subsection*
          {%
            {
            \XSIMmixedcase{\GetExerciseName}%
            \IfInsideSolutionTF
              {
                %\GetExerciseParameter{exercise-name}%
                ~\GetExerciseProperty{counter}.%
              }
              {%
                ~\GetExerciseProperty{counter}
                \GetExercisePropertyT{subtitle}
                  { {\normalfont\itshape\PropertyValue}}%
              }%
            }
          }
      }
      {\end{samepage}}
      
    \xsimsetup{exercise/template = customex}
    
    \begin{document}
        \begin{exercise}
            \lipsum[1-4]
        \end{exercise}
        
        \begin{exercise}
            \lipsum[1]  
        \end{exercise}
\end{document}

The result is Exercise 1 being broken across 2 pages, instead of the desired Exercise 1 on page 1 and Exercise 2 on page 2.

2
  • I have also tried using exercise/pre-hook{/begin{samepage} } and exercise/post-hook which also breaks exercise 1 over 2 pages.
    – JSchon
    Mar 16, 2018 at 22:25
  • Maybe you could use \newenvironment{question}{\begin{samepage}\begin{exercise}}{\end{exercise}\end{samepage}} ?
    – garondal
    Jul 7, 2019 at 22:06

2 Answers 2

0

I get good results with

\xsimsetup{
    exercise/begin-hook  = \begin{samepage},
    exercise/end-hook = \end{samepage},
    solution/begin-hook  = \begin{samepage},
    solution/end-hook = \end{samepage},
}

The solution from @cgnieder with pre-hook/post-hook gave strange effects.

1
  • 1
    What kind of strange effects? And in combination with which templates? Ideally you could post a MWE on xsim's issue tracker?
    – cgnieder
    Jul 1, 2020 at 12:28
0

The easiest way without having to define any template would be

\BeforeBeginEnvironment{exercise}{%
  \begin{samepage}%
}
\AfterEndEnvironment{exercise}{%
  \end{samepage}%
}

Since \samepage “only” does some penalty settings this won't ensure that your exercises always are on the same page. (E.g., this doesn't help for your example).

If you want to be really sure that exercises stay on one page and if you also are sure that exercises never are longer than one page you can use a minipage:

\BeforeBeginEnvironment{exercise}{%
  \noindent\begin{minipage}{\linewidth}%
}
\AfterEndEnvironment{exercise}{%
  \end{minipage}\par
}

Probably even better with some vertical space added:

\documentclass{article}
\usepackage{xsim,lipsum}

\BeforeBeginEnvironment{exercise}{%
  \vspace{3.5ex plus 1ex minus .2ex}
  \noindent\begin{minipage}{\linewidth}%
}
\AfterEndEnvironment{exercise}{%
  \end{minipage}\par
}


\begin{document}

\begin{exercise}
  \lipsum[1-4]
\end{exercise}

\begin{exercise}
  \lipsum[1]
\end{exercise}

\begin{exercise}
  \lipsum[1]
\end{exercise}

\end{document}
2
  • Thanks, that does indeed work. But, the mere inclusion of hyperref, no matter the load order, breaks the result with all minipages on one page and just drifting off the bottom.
    – JSchon
    Jan 13, 2021 at 4:06
  • @JSchon edited with a version that works also with hyperref (but requires a rather recent LaTeX)
    – cgnieder
    Jan 13, 2021 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .