3

I have set up a a tabular environment and inside of it a tikzpicture. How can I adjust the width to the table cell?

\documentclass[a4paper,12pt]{article} % DIN A4

\usepackage[a4paper, portrait, top=25mm, bottom=20mm, right=25mm, left=25mm]{geometry} % Seitenränder \usepackage{amssymb, amsmath} % Mathematische Symbole

\usepackage{tabularx} % Tabellen \usepackage{pgfplots} % Funktionsgraph \pgfplotsset{compat = newest}

\pagenumbering{gobble}

\title{Exponential- und Logartihmusfunktion im Vergleich} \author{me} \begin{document} \maketitle

\noindent\begin{tabularx}{\textwidth}{|X|X|}

\hline \begin{tikzpicture} \begin{axis}[xmin = -10, xmax = 10, ymin = -10, ymax = 10, xtick distance = 2, ytick distance = 2, xticklabel=\empty,yticklabel=\empty, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid style = {lightgray!25}, width = 0.5\textwidth, height = 0.5\textwidth,axis lines = middle,xlabel=$x$,ylabel=$y$, legend style={at={(0.025,0.025)},anchor=south west}, legend cell align={left}] \addplot[domain = -10:10, samples = 200,smooth,thick,blue,grid=major] {2^x)}; \addplot[domain = -10:10, samples = 200,smooth,thin,black,grid=major] {x}; \addlegendentry{$2^x$} \addlegendentry{$x$} \end{axis} \end{tikzpicture} & \begin{tikzpicture} \begin{axis}[xmin = -10, xmax = 10, ymin = -10, ymax = 10, xtick distance = 2, ytick distance = 2, xticklabel=\empty,yticklabel=\empty, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid style = {lightgray!25}, width = 0.5\textwidth, height = 0.5\textwidth,axis lines = middle,xlabel=$x$,ylabel=$y$, legend style={at={(0.025,0.025)},anchor=south west}, legend cell align={left}] \addplot[domain = -10:10, samples = 200,smooth,thick,red,grid=major] {log2(x))}; \addplot[domain = -10:10, samples = 200,smooth,thin,black,grid=major] {x}; \addlegendentry{$log_2(x)$} \addlegendentry{$x$} \end{axis} \end{tikzpicture}\ \hline \end{tabularx} \end{document}

enter image description here

side question: Why do I have to use X for \begin{tabularx}{\textwidth}{|X|X|}?

Zarko
  • 296,517
timlwsk
  • 149

3 Answers3

6

Using tabularx you need to use at least one column of type X, otherwise it will not work properly. In your case I would adopt tikzpicture width to width of table cell and instead of X column types use C derivate from X as \newcolumntype{C}{>{\centering\arraybackslash}X}:

\documentclass[a4paper,12pt]{article} % DIN A4
\usepackage[a4paper, portrait, 
            top=25mm, bottom=20mm, hmargin=25mm]{geometry} % Seitenränder
\usepackage{amssymb, amsmath} % Mathematische Symbole

\usepackage{tabularx} % Tabellen \newcolumntype{C}{>{\centering\arraybackslash}X} \usepackage{pgfplots} % Funktionsgraph \pgfplotsset{compat = 1.17}

\pagenumbering{gobble}

\title{Exponential- und Logartihmusfunktion im Vergleich} \author{me} \begin{document} \maketitle

\begingroup \pgfplotsset{ xmin = -10, xmax = 10, ymin = -10, ymax = 10, xtick distance = 2, ytick distance = 2, xticklabel=\empty, yticklabel=\empty, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid style = {lightgray!25}, width = \linewidth, height = 0.5\textwidth, axis lines = middle, xlabel=$x$, ylabel=$y$, legend pos=north west } \centering \begin{tabularx}{\textwidth}{|C|C|} \hline \begin{tikzpicture} \begin{axis} \addplot[domain = -10:10, samples = 200,smooth,thick,blue] {2^x)}; \addplot[domain = -10:10, samples = 2,smooth,thin,black] {x}; \legend{$2^x$, $x$} \end{axis} \end{tikzpicture} & \begin{tikzpicture} \begin{axis} \addplot[domain = -10:10, samples = 200,smooth,thick,red] {log2(x))}; \addplot[domain = -10:10, samples = 2,smooth,thin,black] {x}; \legend{$\log_2(x)$, $x$} \end{axis} \end{tikzpicture} \ \hline \end{tabularx} \endgroup \end{document}

enter image description here

However, you can adopt cell width to width of tikzpicture by use simple tabular:

\begin{center}
\pgfplotsset{
    xmin = -10, xmax = 10,
    ymin = -10, ymax = 10,
    xtick distance = 2,
    ytick distance = 2,
    xticklabel=\empty,
    yticklabel=\empty,
    grid = both,
    minor tick num = 1,
    major grid style = {lightgray},
    minor grid style = {lightgray!25},
    width = 0.5\textwidth,  % <---
    height = 0.5\textwidth,
    axis lines = middle,
    xlabel=$x$, ylabel=$y$,
    legend pos=north west
            }
\begin{tabular}{|c|c|}
 % ...
\end{tabular}
\end{center}

enter image description here

Addendum: I wonder, why you use table at all, Both images you can draw in parallel without it as ˙groupplot`:

\documentclass[a4paper,12pt]{article} % DIN A4
\usepackage[a4paper, portrait,
            top=25mm, bottom=20mm, hmargin=25mm]{geometry} % Seitenränder

\usepackage{pgfplots} % Funktionsgraph \usepgfplotslibrary{groupplots} \pgfplotsset{compat = 1.17}

\pagenumbering{gobble} \title{Exponential- und Logartihmusfunktion im Vergleich} \author{me}

\begin{document} \maketitle \begin{center} \begin{tikzpicture} \begin{groupplot}[ group style = { group size=2 by 1, horizontal sep=1em, }, xmin = -10, xmax = 10, ymin = -10, ymax = 10, xtick distance = 2, ytick distance = 2, xticklabel=\empty, yticklabel=\empty, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid style = {lightgray!25}, width = 0.5\textwidth,
height = 0.5\textwidth, axis lines = middle, xlabel=$x$, ylabel=$y$, legend pos=north west ] \nextgroupplot \addplot[domain = -10:10, samples = 200,smooth,thick,blue] {2^x)}; \addplot[domain = -10:10, samples = 2,smooth,thin,black] {x}; \legend{$2^x$, $x$} \nextgroupplot \addplot[domain = -10:10, samples = 200,smooth,thick,red] {log2(x))}; \addplot[domain = -10:10, samples = 2,smooth,thin,black] {x}; \legend{$\log_2(x)$, $x$} \end{groupplot} \end{tikzpicture} \end{center} \end{document}

groupplot

Zarko
  • 296,517
4

Since pgfplots doesn't use the exact space, you could make it fit using \resizebox of graphicx:

\resizebox{\textwidth}{!}{%
\begin{tikzpicture}%
...
\end{tikzpicture}}

\documentclass{article} 
\usepackage{tabularx} 
\usepackage{pgfplots} 
\pgfplotsset{compat = newest}

\begin{document}

% https://tex.stackexchange.com/questions/249040 \noindent \begin{tabularx}{\linewidth}{ |>{\hsize=1\hsize}X|% 50% of 2\hsize >{\hsize=1\hsize}X|% 50% of 2\hsize % sum=2.0\hsize for 2 columns } \hline % <-- Left Cell Begins \begin{tikzpicture} \begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$}, width=0.50\textwidth, ] % use TeX as calculator: \addplot {x^2 - x +4}; \end{axis} \end{tikzpicture} & % <-- Next Cell \begin{tikzpicture} \begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$}, width=0.50\textwidth, ] % use TeX as calculator: \addplot {x^2 - x +4}; \end{axis} \end{tikzpicture} \ % <-- End of Row \hline \end{tabularx}

\end{document}

enter image description here

Update:

It looks like width=1\linewidth, is even better than width=0.5\textwidth,.

enter image description here

\documentclass{article} 
\usepackage{tabularx} 
\usepackage{pgfplots} 
\pgfplotsset{compat = newest}

\begin{document}

% https://tex.stackexchange.com/questions/ \noindent \begin{tabularx}{\linewidth}{ |>{\hsize=1\hsize}X|% 50% of 2\hsize >{\hsize=1\hsize}X|% 50% of 2\hsize % sum=2.0\hsize for 2 columns } \hline % <-- Left Cell Begins \begin{tikzpicture} \begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$}, width=1\linewidth, ] % use TeX as calculator: \addplot {x^2 - x +4}; \end{axis} \end{tikzpicture} & % <-- Next Cell \begin{tikzpicture} \begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$}, width=1\linewidth, ] % use TeX as calculator: \addplot {x^2 - x +4}; \end{axis} \end{tikzpicture} \ % <-- End of Row \hline \end{tabularx}

\end{document}

  • 1
    As you ask me (in comment below my answer): advantages of tabularx (in short) is ability to calculate table columns width. FOr this you need to prescribe width of table and use at least one X column type. Later is actually p{<calculated width>} column. These widths you can change with ratios between column width: \hsize=<ratio>\hsize, where you need to take a care that sum of ratios is equal to number of X columns. In OP case you not need to declare ratios between column widths since images have equal width. – Zarko Apr 03 '21 at 00:07
  • @zarko thanks, that is helpful – Dr. Manuel Kuehner Apr 03 '21 at 00:16
3

While this does not technically answer the question, in general the groupplots library is a very good tool to arrange several plots relative to each other. Among other things this also allows you to draw the table lines with TikZ, which offers more control and variations.

\documentclass[a4paper,12pt]{article} % DIN A4

\usepackage[a4paper, portrait, top=25mm, bottom=20mm, right=25mm, left=25mm]{geometry} \usepackage{amssymb, amsmath} % Mathematische Symbole

\usepackage{tabularx} % Tabellen \usepackage{pgfplots} % Funktionsgraph \usepgfplotslibrary{groupplots} \pgfplotsset{compat = newest}

\pagenumbering{gobble}

\title{Exponential- und Logartihmusfunktion im Vergleich} \author{me} \begin{document} \maketitle

\centering \begin{tikzpicture} \begin{groupplot}[group style={group size=2 by 1, horizontal sep=2ex},%<-adjust xmin = -10, xmax = 10, ymin = -10, ymax = 10, xtick distance =2, ytick distance = 2, xticklabel=\empty,yticklabel=\empty, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid style ={lightgray!25}, axis lines =middle,xlabel=$x$,ylabel=$y$, legend style={at={(0.025,0.025)},anchor=south west}, legend cell align={left}, height=7cm,width=8cm, no markers ] \nextgroupplot \addplot[domain = -10:10, samples = 200,smooth,thick,blue,grid=major] {2^x)}; \addplot[domain = -10:10, samples = 200,smooth,thin,black,grid=major] {x}; \addlegendentry{$2^x$} \addlegendentry{$x$} \nextgroupplot \addplot[domain = -10:10, samples = 200,smooth,thick,red,grid=major] {log2(x))}; \addplot[domain = -10:10, samples = 200,smooth,thin,black,grid=major] {x}; \addlegendentry{$\log_2(x)$} \addlegendentry{$x$} \end{groupplot} \path (group c1r1.east) -- coordinate[pos=0.5] (aux) (group c2r1.west); \draw ([xshift=-0.5ex,yshift=-0.5ex]current bounding box.south west) coordinate (sw) rectangle ([xshift=0.5ex,yshift=0.5ex]current bounding box.north east) coordinate (ne) (aux|-sw) -- (aux|-ne); \end{tikzpicture} \end{document}

enter image description here