2

I read about my task here. But I didn't get working it for my needs. 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}

naturally yields two errors described in the linked question. I only wish to load a tex file with two (or probably more) parameters. How could it be done?

Also interesting: An additional path to the file. Let say the file is located in the subdirectory figures/2 of the main directory. To access to the file one have usually type \input{figures/2/compensation}.

Thank you for your help and effort in advance!

Su-47
  • 2,508

1 Answers1

2

You can, but isn't it simpler to use \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}

enter image description here

Short explanation. The file contents is stored in a token list variable that's used as the replacement text of a temporary function. Finally the desired command is defined in terms of this temporary function.

egreg
  • 1,121,712
  • Hello @egreg! Thank you for your answer! I wish to load this file in various documents, that is why I can't use a \newcommand statement. – Su-47 Feb 21 '20 at 14:07