多行环境的宏->如何处理“{%”?

多行环境的宏->如何处理“{%”?

我非常喜欢这个论坛。过去,它帮助了我好几次,只是阅读而已。遗憾的是,对于我遇到的特殊问题,我找不到任何解决方案,所以我决定向社区询问。

我想编写一个宏来帮助创建一个特殊的多行表。表格内部有一个 tikzpicture 环境,这使得创建表格变得更加困难。

以下是一个小例子:

%% Erläuterungen zu den Befehlen erfolgen unter
%% diesem Beispiel.

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{array}
\usepackage{multirow}
\providecommand{\tabularnewline}{\\}

\usepackage{pgfplots} % LaTeX
\usepgfplotslibrary{fillbetween}


\newcommand{\tikzheader[1]}{%           
xlabel={#1}, 
}

% My Macro try
%\starttable{MyyLableName}

\newcommand{\starttable[1]}{%   
\begin{figure}[H]   
            \renewcommand{\arraystretch}{1}                 
            \begin{tabular}{|>{\centering\arraybackslash}m{0.665\textwidth}|>{\centering\arraybackslash}m{0.28\textwidth}|}
\hline
\multirow{16}[16]{*}[-1mm]{%
\begin{tikzpicture}
\begin{axis}
[
legend style={legend pos=north east, font=\footnotesize},
grid = major,
clip=false,
xmin=0,
xmax=8,
ylabel={#1}, 
ymin=0,
ymax=10,
y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
x tick label style={/pgf/number format/fixed },
height=60mm,
width=60mm
]
}}


%\finishtable{MyTableEntries}
\newcommand{\finishtable[1]}{%  
\end{axis}
\end{tikzpicture}}
                   & #1 \\ %\cline{2-2} 
                   &  2 \\ \cline{2-2} 
                   & 3 \\ %\cline{2-2} 
                   & 4 \\ \cline{2-2} 
                   & 5 \\ %\cline{2-2} 
                   & 6 \\ \cline{2-2} 
                   & 7 \\ %\cline{2-2} 
                   & 8 \\ \cline{2-2} 
                   & 9 \\ %\cline{2-2} 
                   & 10 \\ \cline{2-2} 
                   & 11 \\ %\cline{2-2} 
                   & 12 \\ \cline{2-2}                          
                                   & 13 \\ %\cline{2-2} 
                   & 14 \\ \cline{2-2}                              
                   & 15 \\ %\cline{2-2} 
                   & 16\\ \cline{2-2} 
                   &  17 \\ \hline
       \end{tabular}
\caption{Caption}
\end{figure}
}



\begin{document}


\section{Original table}

\begin{figure}[H]   
            \renewcommand{\arraystretch}{1}                 
            \begin{tabular}{|>{\centering\arraybackslash}m{0.665\textwidth}|>{\centering\arraybackslash}m{0.28\textwidth}|}
\hline
\multirow{16}[16]{*}[-1mm]{%
\begin{tikzpicture}
\begin{axis}
[
legend style={legend pos=north east, font=\footnotesize},
grid = major,
clip=false,
xmin=0,
xmax=8,
ylabel={test}, 
ymin=0,
ymax=10,
y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
x tick label style={/pgf/number format/fixed },
height=60mm,
width=60mm
]
%%% black
%\addplot Dummy
%%%
\end{axis}
\end{tikzpicture}}
                   & 1 \\ %\cline{2-2} 
                   &  2 \\ \cline{2-2} 
                   & 3 \\ %\cline{2-2} 
                   & 4 \\ \cline{2-2} 
                   & 5 \\ %\cline{2-2} 
                   & 6 \\ \cline{2-2} 
                   & 7 \\ %\cline{2-2} 
                   & 8 \\ \cline{2-2} 
                   & 9 \\ %\cline{2-2} 
                   & 10 \\ \cline{2-2} 
                   & 11 \\ %\cline{2-2} 
                   & 12 \\ \cline{2-2}                          
                                   & 13 \\ %\cline{2-2} 
                   & 14 \\ \cline{2-2}                              
                   & 15 \\ %\cline{2-2} 
                   & 16\\ \cline{2-2} 
                   &  17 \\ \hline
       \end{tabular}
\caption{Caption}
\end{figure}    


\section{table as Macro with error}

\starttable{MyyLableName}
%\addplot dummy
\finishtable{MyTableEntries}

\end{document}

我的目的是定义一个可以实现这种处理的宏:

\starttable{MyyLableName}
%\addplot dummy
\finishtable{MyTableEntries}

根据以上内容。您有什么解决这个问题的提示吗?我期待您的回复。

答案1

更新

我建议使用环境和仅一个参数作为使用键值结构的绘图选项。这将比仅设置xlabelylabelxmaxymaxlegend poswidth的固定数量的参数更灵活height。而且您不必知道参数的顺序。

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{array}

\usepackage{float}% <- added

\usepackage{pgfplots}
\pgfplotsset{compat=1.14}% <- added

\newcommand\MyTableCaption{}% initialize the command
\newenvironment{MyTable}[1]
  {%
    \def\MyTableCaption{#1}% save the option
    \begin{figure}[H]
    \renewcommand{\arraystretch}{1}
    \begin{tabular}{|@{}c@{}|@{}c@{}|}
      \hline
  }
  {%
      \\ \hline
      \end{tabular}
      \caption{\MyTableCaption}
    \end{figure}%
  }
\newenvironment{MyTablePlot}[1]
  {%
    \begin{tabular}{p{0.68\textwidth}}
      \centering
      \begin{tikzpicture}
      \begin{axis}
        [
          legend style={legend pos=north east, font=\footnotesize},
          grid = major,
          clip=false,
          xmin=0,
          xmax=8,
          ymin=0,
          ymax=10,
          y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
          x tick label style={/pgf/number format/fixed },
          height=60mm,
          width=60mm,
          #1
        ]
  }
  {%
        \end{axis}
      \end{tikzpicture}
    \end{tabular}%
  }
\newenvironment{MyTableValues}
  {\begin{tabular}{p{0.28\textwidth}}}
  {\end{tabular}}
\newcommand\starttable[1]
  {%
    \begin{figure}[H]
    \renewcommand{\arraystretch}{1}
    \begin{tabular}{|@{}c@{}|@{}c@{}|}
      \hline
      \begin{tabular}{p{0.66\textwidth}}
        \centering
        \begin{tikzpicture}
        \begin{axis}
          [
            legend style={legend pos=north east, font=\footnotesize},
            grid = major,
            clip=false,
            xmin=0,
            xmax=8,
            ylabel={#1}, 
            ymin=0,
            ymax=10,
            y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
            x tick label style={/pgf/number format/fixed },
            height=60mm,
            width=60mm
          ]
}

\begin{document}
\section{A Table}
\begin{MyTable}{Caption for this Table}
  \begin{MyTablePlot}
    {
      ylabel={y label},
      xlabel={x label},
      xmin=-2,
      domain=-2:10,
      clip=true
    }
    \addplot[red]{x};
  \end{MyTablePlot}
  &
  \begin{MyTableValues}
      1\\
      2 \\ \hline 
      3 \\
      4 \\ \hline 
      5 \\ 
      6 \\ \hline 
      7 \\ 
      8 \\ \hline 
      9 \\ 
      10 \\ \hline 
      11 \\
      12 \\ \hline
      13 \\
      14 \\ \hline
      15 \\
      16\\ \hline 
      17
  \end{MyTableValues}
\end{MyTable}
\end{document}

在此处输入图片描述


原始答案

我不明白你真正想要实现什么,但没有必要使用\multirow

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{array}
\usepackage{multirow}
\usepackage{float}% <- added

\usepackage{pgfplots}
\pgfplotsset{compat=1.14}% <- added

\newcommand\starttable[1]
  {%
    \begin{figure}[H]
    \renewcommand{\arraystretch}{1}
    \begin{tabular}{|@{}c@{}|@{}c@{}|}
      \hline
      \begin{tabular}{p{0.68\textwidth}}
        \centering
        \begin{tikzpicture}
        \begin{axis}
          [
            legend style={legend pos=north east, font=\footnotesize},
            grid = major,
            clip=false,
            xmin=0,
            xmax=8,
            ylabel={#1}, 
            ymin=0,
            ymax=10,
            y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
            x tick label style={/pgf/number format/fixed },
            height=60mm,
            width=60mm
          ]
}
\newcommand\finishtable[1]
{%
        \end{axis}
      \end{tikzpicture}
    \end{tabular}
    &
    \begin{tabular}{p{0.28\textwidth}}
      #1
    \end{tabular}\\ \hline
  \end{tabular}
  \caption{Caption}
\end{figure}
}

\begin{document}
\section{Original table}
\begin{figure}[H]
  \renewcommand{\arraystretch}{1}
  \begin{tabular}{|>{\centering\arraybackslash}m{0.665\textwidth}|>{\centering\arraybackslash}m{0.28\textwidth}|}
    \hline
    \multirow{16}[16]{*}[-1mm]{%
      \begin{tikzpicture}
        \begin{axis}
          [
            legend style={legend pos=north east, font=\footnotesize},
            grid = major,
            clip=false,
            xmin=0,
            xmax=8,
            ylabel={test}, 
            ymin=0,
            ymax=10,
            y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},fixed},
            x tick label style={/pgf/number format/fixed },
            height=60mm,
            width=60mm
          ]
          %%% black
          %\addplot Dummy
          %%%
        \end{axis}
      \end{tikzpicture}}
    & 1 \\ %\cline{2-2} 
    &  2 \\ \cline{2-2} 
    & 3 \\ %\cline{2-2} 
    & 4 \\ \cline{2-2} 
    & 5 \\ %\cline{2-2} 
    & 6 \\ \cline{2-2} 
    & 7 \\ %\cline{2-2} 
    & 8 \\ \cline{2-2} 
    & 9 \\ %\cline{2-2} 
    & 10 \\ \cline{2-2} 
    & 11 \\ %\cline{2-2} 
    & 12 \\ \cline{2-2}
    & 13 \\ %\cline{2-2} 
    & 14 \\ \cline{2-2}
    & 15 \\ %\cline{2-2} 
    & 16\\ \cline{2-2} 
    &  17 \\ \hline
  \end{tabular}
\caption{Caption}
\end{figure}


\section{table as Macro with error}
\starttable{MyyLableName}
%\addplot dummy
\finishtable{%
  1\\
  2 \\ \hline 
  3 \\
  4 \\ \hline 
  5 \\ 
  6 \\ \hline 
  7 \\ 
  8 \\ \hline 
  9 \\ 
  10 \\ \hline 
  11 \\
  12 \\ \hline
  13 \\
  14 \\ \hline
  15 \\
  16\\ \hline 
  17
}
\end{document}

在此处输入图片描述

相关内容