如何在 newcommand 语句中输入 tex 文件?

如何在 newcommand 语句中输入 tex 文件?

我读了我的任务这里。但我没能满足我的需求。MWE:

\begin{filecontents*}{compensation.tex}
  \begin{tikzpicture}[auto]

    %placing the nodes
    \node[signal] (input) {};
    \node[
          block,
          right = of input
         ] (controller) {linearer Regler};
    \node[
          block,
          path picture = {
                          \draw[double distance = 2pt]
                            (path picture bounding box.south west) rectangle
                            (path picture bounding box.north east);
                         },
          right = of controller
         ] (compensation) {$#1$}; %%% first parameter
    \node[
          block,
          right = of compensation
         ] (system) {$#2$}; %%% second parameter
    \node[
          signal,
          right = of system
         ] (output) {};
    %connecting the nodes
    \draw
      [->] (input) -- node {$e$} (controller);
    \draw
      [->] (controller) -- node {$u_*$} (compensation);
    \draw
      [->] (compensation) -- node {$u$} (system);
    \draw
      [->] (system) -- node {$x$} (output);
    \draw[
          decorate,
          decoration = {
                        brace,
                        amplitude = 5pt
                       }
         ] (.98, .5) -- (6.31, .5)
      node[
           above = 5pt,
           midway
          ] {Rechner};
    \draw[
          decorate,
          decoration = {
                        brace,
                        amplitude = 5pt
                       }
         ] (10, -.5) -- (4.72, -.5)
      node[
           below = 5pt,
           midway
          ] {bzgl.\ $u_*$ lineare Strecke};

  \end{tikzpicture}
\end{filecontents*}

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{
                decorations.pathreplacing,
                positioning
               }
\tikzset{
         signal/.style = coordinate,
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        }
        }
\newcommand*\test[2]{\input{compensation}}

\begin{document}

  \test{()^2}{\dot x = \ldots + \sqrt{u}}

\end{document}

自然会产生链接问题中描述的两个错误。我只希望加载一个包含两个(或大概更多)参数。该怎么做呢?

同样有趣的是:文件的附加路径。假设文件位于figures/2主目录的子目录中。要访问该文件,通常需要键入\input{figures/2/compensation}

提前感谢您的帮助和努力!

答案1

可以,但是使用起来不是更简单吗\newcommand

\begin{filecontents*}{compensation.tex}
  \begin{tikzpicture}[auto]

    %placing the nodes
    \node[signal] (input) {};
    \node[
          block,
          right = of input
         ] (controller) {linearer Regler};
    \node[
          block,
          path picture = {
                          \draw[double distance = 2pt]
                            (path picture bounding box.south west) rectangle
                            (path picture bounding box.north east);
                         },
          right = of controller
         ] (compensation) {$#1$}; %%% first parameter
    \node[
          block,
          right = of compensation
         ] (system) {$#2$}; %%% second parameter
    \node[
          signal,
          right = of system
         ] (output) {};
    %connecting the nodes
    \draw
      [->] (input) -- node {$e$} (controller);
    \draw
      [->] (controller) -- node {$u_*$} (compensation);
    \draw
      [->] (compensation) -- node {$u$} (system);
    \draw
      [->] (system) -- node {$x$} (output);
    \draw[
          decorate,
          decoration = {
                        brace,
                        amplitude = 5pt
                       }
         ] (.98, .5) -- (6.31, .5)
      node[
           above = 5pt,
           midway
          ] {Rechner};
    \draw[
          decorate,
          decoration = {
                        brace,
                        amplitude = 5pt
                       }
         ] (10, -.5) -- (4.72, -.5)
      node[
           below = 5pt,
           midway
          ] {bzgl.\ $u_*$ lineare Strecke};

  \end{tikzpicture}
\end{filecontents*}


\documentclass{article}

\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{
                decorations.pathreplacing,
                positioning
               }
\tikzset{
         signal/.style = coordinate,
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        }
        }

\ExplSyntaxOn

\cs_generate_variant:Nn \cs_set_protected:Nn { cV }

\NewDocumentCommand{\newcommandfromfile}{mO{0}m}
 {% #1 = command, #2 = number of arguments, #3 = file
  \file_get:nnN { #3 } { } \l_tmpa_tl
  \cs_set_protected:cV { __sufortyseven_temp:\prg_replicate:nn { #2 } { n } } \l_tmpa_tl
  \cs_new_eq:Nc #1 { __sufortyseven_temp:\prg_replicate:nn { #2 } { n } }
 }
\ExplSyntaxOff

\newcommandfromfile{\test}[2]{compensation}

\begin{document}

  \test{()^2}{\dot x = \ldots + \sqrt{u}}

\end{document}

在此处输入图片描述

简短解释一下。文件内容存储在一个标记列表变量中,该变量用作临时函数的替换文本。最后根据这个临时函数定义所需的命令。

相关内容