Detailliertes Plotten einer Funktion mit Tikz in Overleaf

Detailliertes Plotten einer Funktion mit Tikz in Overleaf

Ich habe 3 Grafiken mit folgendem Code erstellt:

\documentclass[12pt]{exam}
\usepackage[a4paper, left=2.00cm, right=2.00cm, top=2.00cm, bottom=3.00cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{setspace}
\usepackage[x11names]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[colorlinks=false]{hyperref}
\usepackage{tocloft}
\usepackage{ragged2e}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{babel}

\begin{document}

\begin{tikzpicture}
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
    \path[-, dashed, color = red] (0.5, 2.05) edge (2.8, 2.05) node[anchor=north] {};
    \path[-, dashed, color = red] (2.8, 2.05) edge (2.8, 0.55) node[anchor=north] {};
    \shade[ball color = red] (0.5, 2.05) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 2.05);
\end{tikzpicture}
\begin{tikzpicture}
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
    \path[-, dashed, color = red] (0.5, 0.55) edge (2.8, 0.55) node[anchor=north] {};
    \shade[ball color = red] (2.8, 0.55) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 0.55);
\end{tikzpicture}
\begin{tikzpicture}
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
    \path[-, dashed, color = Green4] (1.65, 0) edge (1.65, 1.05) node[anchor=north] {$\frac{\alpha+\beta}{2}$};
    \path[-, dashed, color = red] (0.5, 1.05) edge (2.8, 1.05) node[anchor=north] {};
    \path[-, dashed, color = red] (2.8, 1.05) edge (2.8, 0.55) node[anchor=north] {};
    \shade[ball color = red] (1.65, 1.05) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 1.05);
\end{tikzpicture}

\end{document}

Bildbeschreibung hier eingeben Ich möchte die drei Grafiken jedoch vertikal ausrichten und darunter etwas Text hinzufügen, wie in diesem Beispiel: Bildbeschreibung hier eingeben

Wie kann ich das machen? Vielen Dank im Voraus

Antwort1

Sie können die X-Achse Ihrer Diagramme ausrichten, indem Sie eine Grundlinie für die TikZ-Bilder angeben. Auf diese Weise müssen Sie sich keine Gedanken darüber machen, ob sie unterschiedliche Größen haben, und ihre Abszissen werden automatisch ausgerichtet (was nicht der Fall wäre, wenn man sie einfach unten ausrichten würde, wie es die üblichen Pakete für Unterabbildungen tun):

\documentclass[12pt]{exam}
\usepackage[a4paper, left=2.00cm, right=2.00cm, top=2.00cm, bottom=3.00cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{setspace}
\usepackage[x11names]{xcolor}
\usepackage{tcolorbox}
\usepackage{amssymb}
\usepackage{tocloft}
\usepackage{ragged2e}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{babel}
\usepackage{subcaption}
\usepackage[colorlinks=false]{hyperref}

\tikzset{mynode/.style={anchor=center,yshift=-2ex}}

\begin{document}
\begin{figure}[htbp]
\hfill\begin{tikzpicture}[baseline={(0,0)}]
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[mynode] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[mynode] {$\beta$};
    \path[-, dashed, color = red] (0.5, 2.05) edge (2.8, 2.05);
    \path[-, dashed, color = red] (2.8, 2.05) edge (2.8, 0.55);
    \shade[ball color = red] (0.5, 2.05) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 2.05);
\end{tikzpicture}%
\hfill\hfill
\begin{tikzpicture}[baseline={(0,0)}]
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[mynode] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[mynode] {$\beta$};
    \path[-, dashed, color = red] (0.5, 0.55) edge (2.8, 0.55);
    \shade[ball color = red] (2.8, 0.55) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 0.55);
\end{tikzpicture}%
\hfill\hfill
\begin{tikzpicture}[baseline={(0,0)}]
    \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
    \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
    \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[mynode] {$\alpha$};
    \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[mynode] {$\beta$};
    \path[-, dashed, color = Green4] (1.65, 0) edge (1.65, 1.05) node[mynode] {$\frac{\alpha+\beta}{2}$};
    \path[-, dashed, color = red] (0.5, 1.05) edge (2.8, 1.05);
    \path[-, dashed, color = red] (2.8, 1.05) edge (2.8, 0.55);
    \shade[ball color = red] (1.65, 1.05) circle (2pt);
    \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 1.05);
\end{tikzpicture}\hfill\mbox{}%

\subcaptionbox{A cat}{\hspace{.33\linewidth}}%
\subcaptionbox{A cat}{\hspace{.33\linewidth}}%
\subcaptionbox{A cat}{\hspace{.33\linewidth}}%
\end{figure}

\end{document}

Bildbeschreibung hier eingeben

Antwort2

Wie das?

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage[x11names]{xcolor}
\usepackage{amssymb}

\begin{document}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.3\textwidth}
    \begin{tikzpicture}
        \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
        \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
        \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
        \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
        \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
        \path[-, dashed, color = red] (0.5, 2.05) edge (2.8, 2.05) node[anchor=north] {};
        \path[-, dashed, color = red] (2.8, 2.05) edge (2.8, 0.55) node[anchor=north] {};
        \shade[ball color = red] (0.5, 2.05) circle (2pt);
        \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 2.05);
    \end{tikzpicture}
    \caption{Caption for the first graphic.}
    \label{fig:sub1}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.3\textwidth}
    \begin{tikzpicture}
        \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
        \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
        \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
        \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
        \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
        \path[-, dashed, color = red] (0.5, 0.55) edge (2.8, 0.55) node[anchor=north] {};
        \shade[ball color = red] (2.8, 0.55) circle (2pt);
        \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 0.55);
    \end{tikzpicture}
    \caption{Caption for the second graphic.}
    \label{fig:sub2}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.3\textwidth}
    \begin{tikzpicture}
        \draw[->] (-0.3, 0) -- (3.3, 0) node[right] {$\mathbb{R}$};
        \draw[->] (0, -0.3) -- (0, 2.5) node[above] {$\mathbb{R}$};
        \draw[thick, domain=0.5:2.8, smooth, variable=\x, blue] plot ({\x}, {sin(2*\x r) + 1.2});
        \path[-, dashed, color = Green4] (0.5, 0) edge (0.5, 2) node[anchor=north] {$\alpha$};
        \path[-, dashed, color = Green4] (2.8, 0) edge (2.8, 0.5) node[anchor=north] {$\beta$};
        \path[-, dashed, color = Green4] (1.65, 0) edge (1.65, 1.05) node[anchor=north] {$\frac{\alpha+\beta}{2}$};
        \path[-, dashed, color = red] (0.5, 1.05) edge (2.8, 1.05) node[anchor=north] {};
        \path[-, dashed, color = red] (2.8, 1.05) edge (2.8, 0.55) node[anchor=north] {};
        \shade[ball color = red] (1.65, 1.05) circle (2pt);
        \fill[OrangeRed1, opacity = 0.15] (0.5, 0) rectangle (2.8, 1.05);
    \end{tikzpicture}
    \caption{Caption for the third graphic.}
    \label{fig:sub3}
  \end{subfigure}
  \caption{Main caption for the three graphics.}
  \label{fig:main}
\end{figure}

Some text below the graphics.

\end{document}

Bildbeschreibung hier eingeben

Sie können die Breite jeder Unterabbildung anpassen, indem Sie den Wert von ändern 0.3\textwidth. Die \hfillBefehle verteilen die Unterabbildungen horizontal.

Antwort3

Zeichnen Sie die Ordinaten neu, sodass alle Bilder die gleiche Höhe haben:

\documentclass{article}
\usepackage[font=small, 
            justification=raggedright]{subcaption}
\usepackage{tikz}
\usepackage[x11names]{xcolor}
\usepackage{nccmath,amssymb}

\begin{document}
    \begin{figure}[htbp]
    \centering
\tikzset{
    tl/.style = {anchor=center, yshift=-2.4ex},
    ln/.style = {draw, color=#1, densely dashed},
  Ball/.style = {ball color = red},
        }
\newcommand\axis{
    \draw[->] (-0.2,0) -- (3.2, 0) node[right] {$\mathbb{R}$};
    \draw[->] (0,-0.8) + (0,0.6) -- (0, 2.5) node[above] {$\mathbb{R}$};
                }
\pgfmathsetmacro{\ya}{sin(2*0.5 r) + 1.2}           % 2*0.5 +1.2
\pgfmathsetmacro{\yb}{sin(2*2.8 r) + 1.2}           % 2*2.8  + 1-2
\pgfmathsetmacro{\yc}{sin(2*(0.5+2.8)/2  r) + 1.2}  % 2*(0.5+2.8)/2 +1.2
%%%% images
\subfloat[Caption for the first graphic.    \label{subfig:a}]%
{% 
    \begin{tikzpicture}
\axis
\draw[thick, blue, domain=0.5:2.8, samples=100] plot (\x, {sin(2*\x r) + 1.2});

\path[ln=Green4](0.5, 0) edge ++ (0,\ya) node[tl] {$\alpha$}
                (2.8, 0) edge ++ (0,\yb) node[tl] {$\beta$};
\path[ln=red]   (0.5, \ya)  -|  (2.8, \yb);
\shade[Ball]    (0.5, \ya) circle[radius=2pt];
\fill[OrangeRed1, opacity = 0.15] (0.5,0) rectangle (2.8, \ya);
    \end{tikzpicture}%
}\hfill
\subfloat[Caption for the second graphic.    \label{subfig:b}]%
{% 
    \begin{tikzpicture}
\axis
\draw[thick, blue, domain=0.5:2.8, samples=100] plot (\x, {sin(2*\x r) + 1.2});

\path[ln=Green4](0.5, 0) edge ++ (0,\ya) node[tl] {$\alpha$}
                (2.8, 0) edge ++ (0,\yb) node[tl] {$\beta$};
\path[ln=red]   (0.5, \yb)  --  (2.8, \yb);
\shade[Ball]    (2.8, \yb) circle[radius=2pt];
\fill[OrangeRed1, opacity = 0.15] (0.5,0) rectangle (2.8, \yb);
    \end{tikzpicture}%
}\hfill
\subfloat[Caption for the second graphic.    \label{subfig:c}]%
{% 
    \begin{tikzpicture}
\axis
\draw[thick, blue, domain=0.5:2.8, samples=100] plot (\x, {sin(2*\x r) + 1.2});

\path[ln=Green4](0.5, 0) edge ++ (0,\ya) node[tl] {$\alpha$}
                (2.8, 0) edge ++ (0,\yb) node[tl] {$\beta$}
                (1.65,0) edge ++ (0,\yc) node[tl] {$\mfrac{\alpha+\beta}{2}$};
\path[ln=red]   (0.5, \yc)  -|  (2.8, \yb);
\shade[Ball]    (1.65,\yc) circle[radius=2pt];
\fill[OrangeRed1, opacity = 0.15] (0.5,0) rectangle (2.8, \yc);
    \end{tikzpicture}%
}
\caption{Main caption for the three graphics.}
\label{fig:main}
    \end{figure}
\end{document}

Bildbeschreibung hier eingeben

Kommentare:
Im Vergleich zu Ihrer und anderen Lösungen weist das obige MWE die folgenden Hauptunterschiede auf:

  • definierter Befehl zum Zeichnen von Koordinatensystemen (damit alle Bilder gleich hoch sind),
  • Gemeinsame Definition von Bildelementstilen
  • Definierte Makros zur Berechnung der in Bildern verwendeten Y-Koordinaten,
  • Verwendete \subfloatUmgebungen, wie sie durch subcaptionPaketversion 3.1 oder neuer emuliert werden.

verwandte Informationen