3

Here is my tex-code:

\documentclass[12pt]{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage[yyyymmdd]{datetime}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}

\usepackage{cleveref}
\usepackage[most]{tcolorbox}

\newtcbtheorem{Theorem}{Theorem}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!75!black,
  fonttitle=\bfseries,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!75!black,
    colframe=blue!75!black,
  } 
}{thm}
\newtcbtheorem[no counter]{Example}{Eksempel}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!25,
  fonttitle=\bfseries,
  coltitle=black,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!25,
    colframe=blue!25,
  } 
}{prf}


\begin{document}

\section*{Funktioner}

\subsection*{Hvad er en graf?}

\noindent En \textbf{graf} er billedet af en funktion, som man tegner ind i et koordinatsystem.  \\

\begin{Example}{}{}
\noindent Vi ønsker, at tegne grafen for funktionen $y=4\cdot x$. Denne funktion beskriver sammenhængen mellem $x$ og $y$. Vi vælger først forskellige $x$-værdier og finder deres $y$-værdier: \\
\begin{center}
\begin{tabular}{|c|c|} \hline
$x$ & $y$  \\ \hline
0 & 0  \\
1 & 4  \\
2 & 8  \\ 
3 & 12  \\ 
4 & 16  \\ \hline
\end{tabular}
\end{center}

\noindent Ved at bruge $x$ og $y$-værdierne kan vi nu tegne grafen (billedet) af funktionen $y=4\cdot x$:
\end{Example}

\begin{figure}[ht!]
 \centering
  \includegraphics[width=4cm]{billede.png}
  \label{fig:boat1}
\end{figure}

\end{document}

I would like my image to be inside the example-box but latex keeps showing an error. How do I fix this?

billede

Thanks.

2
3

If you want your image inside the example box, you should not use a figure environment, because a figure environment allows your image to "float" through your document, which is contra productive if you want it at a specific place.

In case you might want a caption for your image, you could use the \captionof macro from the caption package.

\documentclass[12pt]{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage[yyyymmdd]{datetime}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}

\usepackage{cleveref}
\usepackage[most]{tcolorbox}

\newtcbtheorem{Theorem}{Theorem}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!75!black,
  fonttitle=\bfseries,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!75!black,
    colframe=blue!75!black,
  } 
}{thm}
\newtcbtheorem[no counter]{Example}{Eksempel}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!25,
  fonttitle=\bfseries,
  coltitle=black,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!25,
    colframe=blue!25,
  } 
}{prf}


\begin{document}

\section*{Funktioner}

\subsection*{Hvad er en graf?}

\noindent En \textbf{graf} er billedet af en funktion, som man tegner ind i et koordinatsystem.  \\

\begin{Example}{}{}
\noindent Vi ønsker, at tegne grafen for funktionen $y=4\cdot x$. Denne funktion beskriver sammenhængen mellem $x$ og $y$. Vi vælger først forskellige $x$-værdier og finder deres $y$-værdier: \\
\begin{center}
\begin{tabular}{|c|c|} \hline
$x$ & $y$  \\ \hline
0 & 0  \\
1 & 4  \\
2 & 8  \\ 
3 & 12  \\ 
4 & 16  \\ \hline
\end{tabular}
\end{center}

\noindent Ved at bruge $x$ og $y$-værdierne kan vi nu tegne grafen (billedet) af funktionen $y=4\cdot x$:

\centering
\includegraphics[width=4cm]{example-image}
\end{Example}

\end{document}

enter image description here

0
2

Welcome in TeX.SE.

The error comes from the float environment figure. Floats may not to be inside any fixed environment as example in your example. You can simply remove the figure environment and add:

\begin{center}
        \includegraphics[width=4cm]{billede.png}
\end{center}

at the end of your example environment.

\documentclass[12pt]{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage[yyyymmdd]{datetime}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}

\usepackage{cleveref}
\usepackage[most]{tcolorbox}

\newtcbtheorem{Theorem}{Theorem}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!75!black,
  fonttitle=\bfseries,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!75!black,
    colframe=blue!75!black,
  } 
}{thm}
\newtcbtheorem[no counter]{Example}{Eksempel}{
  enhanced,
  sharp corners,
  attach boxed title to top left={
    yshifttext=-1mm
  },
  colback=white,
  colframe=blue!25,
  fonttitle=\bfseries,
  coltitle=black,
  boxed title style={
    sharp corners,
    size=small,
    colback=blue!25,
    colframe=blue!25,
  } 
}{prf}


\begin{document}

\section*{Funktioner}

\subsection*{Hvad er en graf?}

\noindent En \textbf{graf} er billedet af en funktion, som man tegner ind i et koordinatsystem.  \\

\begin{Example}{}{}
\noindent Vi ønsker, at tegne grafen for funktionen $y=4\cdot x$. Denne funktion beskriver sammenhængen mellem $x$ og $y$. Vi vælger først forskellige $x$-værdier og finder deres $y$-værdier: \\
\begin{center}
\begin{tabular}{|c|c|} \hline
$x$ & $y$  \\ \hline
0 & 0  \\
1 & 4  \\
2 & 8  \\ 
3 & 12  \\ 
4 & 16  \\ \hline
\end{tabular}
\end{center}

\noindent Ved at bruge $x$ og $y$-værdierne kan vi nu tegne grafen (billedet) af funktionen $y=4\cdot x$:


\begin{center}
        \includegraphics[width=4cm]{billede.png}
\end{center}

\end{Example}

\end{document}
4
  • a figure is float environment as well a table is. both should not be included in any box... – Zarko Nov 2 '18 at 9:59
  • @Zarko yes, you are right. My answer mentions otherwise ? I can edit to avoid misunderstanding. – Piroooh Nov 2 '18 at 10:06
  • i took liberty and extend your explanation in you answer with my comment (instead of you). – Zarko Nov 2 '18 at 11:51
  • @Zarko feel free to edit it. I just wonder what i wrote wrong in my answer about the floating environments. I mentioned precisely that the figure environment is not compatible with environments like example. Maybe i had to be more precise in the future ? Thanks ! – Piroooh Nov 2 '18 at 12:38

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.