# Limit tcolorbox to the width of the page

In another thread I asked for a little help with setting up an own solution environment to use in student assignment-sheets.

We there checked a boolean (if it's the solution sheet or only the assignment sheet for the students) and if the boolean returns to true, the answers inside the solution environment are printed. (Code is inside the MWE)

As the questions are set up in an enumerate environment, the questions and the answers are indented. The little issue I now have, is that the solution-container (a tcolorbox) is wider than the linewidth. If I indent a second time it's even bigger...

How can I restrict this environment to the real \linewidth?

Here is a MWE:

\documentclass[12pt]{article}

% %%%
% Packages
% %%%
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\geometry{verbose, a4paper, tmargin=3cm, bmargin=3cm, lmargin=2.5cm, rmargin=2.5cm}
\usepackage{lipsum, enumerate, ifthen, tcolorbox}
\tcbuselibrary{breakable}

% %%%
% Variable Settings
% %%%%
\newboolean{solution}
\setboolean{solution}{true}

% %%%
% Environments / Commandy
% %%%%
\newcommand{\Problem}[1]{
{
\vspace*{0.5cm}
\textsf{\textbf{Problem #1}}
\vspace*{0.2cm}
}
}

\newenvironment{solution}
{
\ifthenelse{\boolean{solution}}{
\tcolorbox[breakable, width=\textwidth, colframe=red, colback=white]
}{}
}
{
\ifthenelse{\boolean{solution}}{
\endtcolorbox
\vspace*{-\baselineskip}
}{}
}

% %%%
% Document
% %%%%
\begin{document}

\begin{center}
\LARGE \sf \textbf{MWE}
\end{center}
\vspace*{0.2cm}

\Problem{1: Foo}
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
\begin{solution}
\lipsum[4-5]
\end{solution}

\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
\begin{solution}
\lipsum[4-4]
\end{solution}
\end{enumerate}
\end{enumerate}

\end{document}


I used the showframe attribute of the geometry-package to show the real \linewidth and where my environment is. It's clear, that the overlapping is exactly the same as the indention of the enumerate.

Thank you very much for your help.

• You might add the link to the other thread... – Skillmon Jul 24 '17 at 20:28
• @Skillmon Done! – Sam Jul 24 '17 at 20:31

In your question you already talk about \linewidth -- just use it instead of \textwidth:

\documentclass[12pt]{article}

% %%%
% Packages
% %%%
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\geometry{verbose, a4paper, tmargin=3cm, bmargin=3cm, lmargin=2.5cm, rmargin=2.5cm}
\usepackage{lipsum, enumerate, ifthen, tcolorbox}
\tcbuselibrary{breakable}

% %%%
% Variable Settings
% %%%%
\newboolean{solution}
\setboolean{solution}{true}

% %%%
% Environments / Commandy
% %%%%
\newcommand{\Problem}[1]{
{
\vspace*{0.5cm}
\textsf{\textbf{Problem #1}}
\vspace*{0.2cm}
}
}

\newenvironment{solution}
{
\ifthenelse{\boolean{solution}}{
\tcolorbox[breakable, width=\linewidth, colframe=red, colback=white]
}{}
}
{
\ifthenelse{\boolean{solution}}{
\endtcolorbox
\vspace*{-\baselineskip}
}{}
}

% %%%
% Document
% %%%%
\begin{document}

\begin{center}
\LARGE \sf \textbf{MWE}
\end{center}
\vspace*{0.2cm}

\Problem{1: Foo}
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
\begin{solution}
\lipsum[4-5]
\end{solution}

\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
\begin{solution}
\lipsum[4-4]
\end{solution}
\end{enumerate}
\end{enumerate}

\end{document}


• Oh dear, I'm dumb... Thank you my name fellow! :) – Sam Jul 24 '17 at 20:39
• And I was creating an answer which calculates the \linewidth based on the \leftmargin and the \textwidth, man I'm dumb... – Skillmon Jul 24 '17 at 20:42
• @Sam You're welcome! Now that this problem is solved, can you please go back to your previous ones and look if the answers solve your problems and accept them, if they do? – samcarter_is_at_topanswers.xyz Jul 24 '17 at 20:43
• @Skillmon Your not sam enough for this question :) – samcarter_is_at_topanswers.xyz Jul 24 '17 at 20:43
• @samcarter Obviously not, this feels horrible. I didn't even recognize that it's the \linewidth what I'm calculating (though my results looked fabulous! -- well, exactly like yours with just 3 lines more...) – Skillmon Jul 24 '17 at 20:45