10

I am retyping some old exams into LaTeX, and I encountered the page below

Old exam

Now with some clever commands, and whatnot I was able to create something almost identical. Though the spacing is a tad off, and my equation is a tad small. I also had to use some hacks and ugly code to produce the result

Does anyone have a better cleaner way to do this ?

Here is almost the exact code I am using for typesetting these exams. Only difference is that all the "junk" in the header is placed i sty. files. So y header contains only 3-5 input files. Here I included everything that is needed to run the code. Some improvements to the code below would be great.

\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}

\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} }

\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{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}

Below is a minimal example, barely enough to produce the page above.

\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage[dvipsnames*,svgnames]{xcolor}
\usepackage{mathtools}

\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? 

\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}

Here is how my output looks

My output

Question(s)

  • Is there an easier way, that also looks better for creating the blue box?
  • Are there any smal improvements that could be done to my "long" code?
  • Yesterday I uploaded mdframed v1.0. There is an example which produces your output. – Marco Daniel Nov 14 '11 at 6:08
  • You mention that your formula was a bit small: to fix that use \dbinom instead of \binom. This needs amsmath which is already included by `mathtools'. – Peter Grill Nov 14 '11 at 18:18
10

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}

Result

  • It seems as both of these solutions deppends on that I know how wide and tall the box is supposed to be. Is there any way to adjust the height and width of the box automaticaly? Thanks a lot for your answer =) – N3buchadnezzar Nov 14 '11 at 9:12
  • Yes, use the varwidth environment from the package with the same name. I actually just now release a new version of adjustbox which has a varwidth key, but you can use it without it as well. – Martin Scharrer Nov 14 '11 at 9:39
  • @N3buchadnezzar: See my updated answer. The varwidth environment takes a maximal width argument, but uses the normal width of the content. – Martin Scharrer Nov 14 '11 at 9:46
  • Really cool. Thanks a bunch! Will look into that package. Does this also change the height of the box? I am not too conserned about page breaks. As these boxes will always range from half a page to a sentence. – N3buchadnezzar Nov 14 '11 at 9:51
  • varwidth accepts the same arguments like minipage or \parbox, so you can also give an optional height argument. However, if you simply want to add some height, i.e. vertical padding, I would use the margin key of adjustbox, which can also be used multiple times. – Martin Scharrer Nov 14 '11 at 9:56
7

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}

enter image description here

  • It seems as both of these solutions deppends on that I know how wide and tall the box is supposed to be. Is there any way to adjust the height and width of the box automaticaly? Thanks a lot for your answer =) – N3buchadnezzar Nov 14 '11 at 9:13
3

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}

enter image description here

1
\newenvironment{bluebox}
  {\begin{lrbox}{0}\begin{tabular}{@{}l@{}}}
  {\end{tabular}\end{lrbox}%
   \setlength{\fboxsep}{12pt}% adjust at will
  {\centering\fcolorbox{black}{LightSteelBlue}{\usebox{0}}\par}}

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.