1
\documentclass{book}
\title{Introduction to Latex}
\author{Me}
\date{}

\usepackage[dvipsnames]{xcolor}
\usepackage{exsheets}
\SetupExSheets{counter-within={chapter}}
\newenvironment{myquestion}
  {\question[name={myExercise}]}
  {\endquestion}


\newlength{\currentparindent}
\newenvironment{myset}
    {   \setlength{\currentparindent}{\parindent}
        \par
        \centering
        \begin{minipage}{\textwidth}
        \setlength{\parindent}{\currentparindent}
         \noindent\colorbox{Gray!50}{Exercise \thesubsection  }
        {\color{Black}   \hrule height 0.1ex} \vspace{0.5ex}}
    {

       \end{minipage}
    }  

\begin{document}
    \chapter{first}
        \section{start}
            \subsection{install}
                \begin{myset}
                 \begin{myquestion}
                Review the documentation for your compiler and determine what file naming convention it uses. Compile and Run the main program from page 2.
            \end{myquestion}
                \end{myset}

\end{document} 

Two question: First: I want to make a border for my environment like this enter image description here

Second: How to stretch the colorbox to the end of the line. I know there is an answer to the second question Stretching colorbox to the end of line

But the accepted answer will make the text below the colorbox away from the colorbox, which I don't want.

  • Why don't you use tcolorbox? That would be easier. – TeXnician Sep 16 '17 at 16:06
  • 1
    @TeXnician: I thought that too, but there's an error message when trying to wrap a tcolorbox around it: \__driver_color_pickup_aux:w has an extra }. I encountered the same error message when trying to use tasks within tcolorbox recently`... – user31729 Sep 16 '17 at 16:07
  • @ChristianHupfer I know that tcb is okay with xsim, but never tried with exsheets. Probably an issue of wrapping in so many environments. – TeXnician Sep 16 '17 at 16:09
  • @TeXnician: I have not used xsim yet – user31729 Sep 16 '17 at 16:12
4

Here's a conceptually different version using xsim and tcolorbox.

xsim

\documentclass{book}
\title{Introduction to Latex}
\author{Me}
\date{}

\usepackage{xsim}
\usepackage[skins]{tcolorbox}
\xsimsetup{solution/print=false}
\DeclareExerciseEnvironmentTemplate{tcolorbox}{%
    \tcolorbox[enhanced,left=2pt,right=2pt,title=\GetExerciseName~\thesubsection,% append .\GetExerciseProperty{counter} to count inside subsection,
        colback=white,colframe=black,colbacktitle=gray!50,coltitle=black,sharp corners=all]%
}{\endtcolorbox}
\DeclareExerciseType{myset}{
    exercise-env=myset,
    solution-env=mysetsol,
    exercise-name=Exercise,
    solution-name=Solution,
    exercise-template=tcolorbox,
    solution-template=tcolorbox,
    within=chapter,
    the-counter={\arabic{myset}},
}

\newcounter{myquestion}[myset]
\newenvironment{myquestion}
  {\refstepcounter{myquestion}\textbf{Text here \themyquestion}\par}{}

\begin{document}
    \chapter{first}
        \section{start}
            \subsection{install}
                \begin{myset}
                 \begin{myquestion}
                Review the documentation for your compiler and determine what file naming convention it uses. Compile and Run the main program from page 2.
            \end{myquestion}
                \end{myset}

\end{document} 
3

Like this? These frames, which rely on the framed package, can break across pages

\documentclass{book}
\title{Introduction to Latex}
\author{Me}
\date{}
\usepackage{framed}

\usepackage[svgnames, dvipsnames]{xcolor}
\usepackage{exsheets}
\SetupExSheets{counter-within={chapter}}
\newenvironment{myquestion}
{\question[name={myExercise}]}
{\endquestion}

\newlength{\currentparindent}
\newenvironment{myset}
{\setlength{\currentparindent}{\parindent}
\setlength{\FrameRule}{0.8pt}
\par
%\centering
\framed
\setlength{\parindent}{\currentparindent}\vspace*{-\FrameSep}
\noindent\hspace*{-\dimexpr\FrameSep+\FrameRule}\rlap{\rule[-0.8ex]{\dimexpr\linewidth+2\FrameSep+2\FrameRule}{0.1ex}}
\hspace*{\dimexpr2\FrameRule-\fontdimen2\font}
\colorbox{Gainsboro!60!Lavender}{%
\makebox[\dimexpr\textwidth+\FrameSep+\fontdimen2\font][l]%
{\hspace{\dimexpr\FrameSep+2\FrameRule-\fontdimen2\font} Exercise \thesubsection}
}%
{\vspace{-2ex}}
}
{
\endframed
}

\begin{document}

\chapter{first}
\section{start}
\subsection{install}
\begin{myset}
  \begin{myquestion}
    Review the documentation for your compiler and determine what file naming convention it uses. Compile and Run the main program from page 2.
  \end{myquestion}
\end{myset}

\end{document} 

enter image description here

  • No, the text below get far away. And I want a border. – K.Robert Sep 16 '17 at 16:29
  • Do you mean a frame? – Bernard Sep 16 '17 at 16:30
  • Yes, and I don't want the text below get far away. – K.Robert Sep 16 '17 at 16:31
  • @K.Robert: I've updated my answer. – Bernard Sep 16 '17 at 18:32
  • Could add a few comments to your code? Thanks. – K.Robert Sep 17 '17 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.