如何让 tikzpicture 填满整个表格单元格?

如何让 tikzpicture 填满整个表格单元格?

我已经设置了一个tabular环境,并且在其中有一个tikzpicture。如何调整表格单元格的宽度?

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

在此处输入图片描述

附带问题:为什么我必须使用Xfor \begin{tabularx}{\textwidth}{|X|X|}

答案1

使用时tabularx您需要使用至少一个类型的列X,否则它将无法正常工作。 对于您的情况,我将采用tikzpicture宽度为表格单元格的宽度,并使用派生自以下X类型的列:CX\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}

在此处输入图片描述

tikzpicture但是,您可以使用简单的方法将单元格宽度调整为的宽度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}

在此处输入图片描述

附录: 我想知道,你为什么要使用表格,你可以像˙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} 

群体图

答案2

由于 pgfplots 不使用精确的空间,因此您可以使用 graphicx 的 \resizebox 使其适合:

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

在此处输入图片描述

更新:

看上去width=1\linewidth,比 还要好width=0.5\textwidth,

在此处输入图片描述

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

答案3

虽然这在技术上并没有回答这个问题,但总的来说,这个groupplots库是一个很好的工具,可以安排几个图的相互关系。除此之外,它还允许您使用 Ti 绘制表格线Z,提供更多的控制和变化。

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

在此处输入图片描述

相关内容