# How to input a tex file in a newcommand statement?

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!

## 1 Answer

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
\tl_set_from_file:Nnn \l_tmpa_tl {} { #3 }
\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}


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.

• 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