Best way to create colored text boxes
Here a way to define such a box using adjustbox
. See the manual for more options.
\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage[dvipsnames*,svgnames]{xcolor}
\usepackage{mathtools}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{adjustbox}
\usepackage{varwidth}
\newcounter{tittel}
\setcounter{tittel}{0}
\newcounter{problem}
\setcounter{problem}{0}
\newcounter{alternative}
\setcounter{alternative}{0}
\newcounter{navn}[problem]
\renewcommand{\thenavn}{\alph{navn}}
\newcommand{\navn}{\stepcounter{navn}\paragraph*{\thenavn)}}
\setcounter{navn}{0}
\newcommand{\NR}[1]
{ \vspace{5mm} \begin{minipage}[t]{0.051 \textwidth}
\navn \hspace{0pt}
\end{minipage}
\begin{minipage}[t]{0.949\textwidth}
#1
\end{minipage}
\vspace{-0.3cm}
}
\newif\iffirstalt
\NewDocumentCommand{\Oppgave}{m o}{%
\IfNoValueTF{#2}{\setcounter{alternative}{0}\stepcounter{problem} \firstaltfalse}%
{\stepcounter{alternative}\iffirstalt\else\stepcounter{problem} \firstalttrue\fi}
\section*{Oppgave \arabic{problem}%
{\normalfont\IfNoValueTF{#2}{}{~Alternative \Roman{alternative}\ }
\normalsize (#1 poeng)}%
\addcontentsline{toc}{section}{Oppgave \arabic{problem} }}
\vspace{3mm} }
\newenvironment{bluebox}{%
\noindent
\adjustbox{innerenv={varwidth}[c]{0.9\linewidth},margin=\fboxsep+.25cm \fboxsep+.2cm,bgcolor=LightSteelBlue,frame,center}\bgroup
}{%
\egroup
}
\begin{document}
\Oppgave{5}
En gruppe på $8$ elever besår av like mange gutter som jenter. Vi trekker tilfeldig ut 8 elever.
\NR{ Hva er sannsynligheten for å trekke ut $2$ gutter og $1$ jente? }
\NR{ Hva er sannsynligheten for å trekke ut minst $1$ jente? }
\begin{bluebox}
Hypergeometrisk sannsynlighetsfordeling: $\displaystyle P(X=x)=\dfrac{\binom{m}{k}\binom{n-m}{r-k}}{\binom{n}{r}}$ \\
$m$ elementer i $D$. $n-m$ elementer i $\bar{D}$ \\
$r$ elementer trekkes tilfeldig. \\
$X$ er antall elementer som trekkes fra $D$
\end{bluebox}
\end{document}
Here a solution with mdframed
. Other possibilities are the packages adjustbox
or you simple use tikz
(no page breaks available).
The new version is uploaded to CTAN. If you want to test the current example the package can be downloaded here (include also the new documentations)
According to your numbering of paragraphs I would create a new command which does it for you.
\documentclass{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage{amsmath}
\usepackage[dvipsnames*,svgnames]{xcolor}
\usepackage[framemethod=default]{mdframed}
\mdfdefinestyle{NR}{skipabove=\topskip,skipbelow=\topskip,%
,align=center,%
innerleftmargin=.25cm,linecolor=black,%
linewidth=2pt,backgroundcolor=LightSteelBlue}
\newmdenv[style=NR]{NR}
\newcounter{myparagraph}
\setcounter{myparagraph}{0}
\newcommand\myparagraph[1]{\stepcounter{myparagraph}\paragraph*{\alph{myparagraph})\quad#1}}
\usepackage{showframe}
\begin{document}
\myparagraph{Hva er sannsynligheten for å trekke ut $2$ gutter og $1$ jente?}
\myparagraph{Hva er sannsynligheten for å trekke ut minst $1$ jente?}
\begin{NR}[userdefinedwidth=11cm]
Hypergeometrisk sannsynlighetsfordeling: $\displaystyle P(X=x)=\dfrac{\binom{m}{k}\binom{n-m}{r-k}}{\binom{n}{r}}$ \\
$m$ elementer i $D$. $n-m$ elementer i $\bar{D}$ \\
$r$ elementer trekkes tilfeldig. \\
$X$ er antall elementer som trekkes fra $D$
\end{NR}
\end{document}
A solution with varwidth
and tcolorbox
, which is there almost out of the box, defining a special bluebox
macro:
This declares the \bluebox
macro having one optional and one mandatory argument, with blue background, center alignment with respect to page and using at most 0.8\linewidth
as width and having no frame.
Other options can be given with the optional argument, see the yellow
background for the bluebox
;-)
\newtcbox{bluebox}[1][]{enhanced jigsaw,
sharp corners,
colback=LightSteelBlue,
center,
varwidth upper=0.8\linewidth,
frame hidden,
#1}
Full code
\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage[dvipsnames*,svgnames]{xcolor}
\usepackage{mathtools}
\usepackage{varwidth}
\usepackage[most]{tcolorbox}
\newtcbox{bluebox}[1][]{enhanced jigsaw,
sharp corners,
colback=LightSteelBlue,
center,
varwidth upper=0.8\linewidth,
frame hidden,
#1}
\begin{document}
\section*{Oppgave 2 \normalsize \normalfont (5 poeng)}
En gruppe på $8$ elever besår av like mange gutter som jenter. Vi trekker tilfeldig ut 8 elever.
\paragraph*{a)} Hva er sannsynligheten for å trekke ut $2$ gutter og $1$ jente?
\paragraph*{b)} Hva er sannsynligheten for å trekke ut minst $1$ jente?
\bluebox{%
Hypergeometrisk sannsynlighetsfordeling: $\displaystyle P(X=x)=\dfrac{\binom{m}{k}\binom{n-m}{r-k}}{\binom{n}{r}}$
$m$ elementer i $D$. $n-m$ elementer i $\bar{D}$ \\
$r$ elementer trekkes tilfeldig. \\
$X$ er antall elementer som trekkes fra $D$ %
}
\bluebox[colback=yellow!60!white]{%
Hypergeometrisk sannsynlighetsfordeling: $\displaystyle P(X=x)=\dfrac{\binom{m}{k}\binom{n-m}{r-k}}{\binom{n}{r}}$
$m$ elementer i $D$. $n-m$ elementer i $\bar{D}$ \\
$r$ elementer trekkes tilfeldig. \\
$X$ er antall elementer som trekkes fra $D$ %
}
\begin{center}
\noindent\fcolorbox{black}{LightSteelBlue}{%
\parbox[t][3cm][c]{11cm}{ \hspace*{0.25cm} Hypergeometrisk sannsynlighetsfordeling: $\displaystyle P(X=x)=\dfrac{\binom{m}{k}\binom{n-m}{r-k}}{\binom{n}{r}}$ \\
\hspace*{0.25cm} $m$ elementer i $D$. $n-m$ elementer i $\bar{D}$ \\
\hspace*{0.25cm} $r$ elementer trekkes tilfeldig. \\
\hspace*{0.25cm} $X$ er antall elementer som trekkes fra $D$ }%
}%
\end{center}
\end{document}