# Parameterized input of file inside tikz

I have looked into a few threads about passing arguments to \input command but haven't found much. Any help with my current predicament (or feedback of an alternative approach) would be appreciated! My current setup (working, but not ideal) is:

Relevant part from main.tex

\begin{tikzpicture}[scale=.3]
\input{grid}
%some other shapes
\end{tikzpicture}


grid.tex

\draw[black!20!white] (0,0) grid (20, 20);
\fill (0,0) circle (2pt) node[below left] {(0,200)};
\fill (20,0) circle (2pt) node[below right] {(200,200)};
\fill (0,20) circle (2pt) node[above left] {(0,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\tiny
\foreach \x in {20, 40, ..., 180} {
\fill (\x/10,20) circle (2pt) node[above] {\x};
}
\foreach \y in {20, 40, ..., 180} {
\fill (0,20-\y/10) circle (2pt) node[left] {\y};
}


Although I'd like to customize the grid. Something like -

\pgfmathsetmacro{\inc}{#1/10}
\draw[black!20!white] (0,0) grid (\inc,\inc);
\fill (0,0) circle (2pt) node[below left] {(0,#1)};
\fill (\inc,0) circle (2pt) node[below right] {(#1,#1)};
\fill (0,\inc) circle (2pt) node[above left] {(0,0)};
\fill (\inc,\inc) circle (2pt) node[above right] {(#1,0)};
\fill (\inc,\inc) circle (2pt) node[above right] {(#1,0)};
\tiny
\foreach \x in {#2, #2*2, ..., #1 - #2} {
\fill (\x/10,\inc) circle (2pt) node[above] {\x};
}
\foreach \y in {#3, #3*2, ..., #1 - #3} {
\fill (0,\inc-\y/10) circle (2pt) node[left] {\y};
}


And input it in main.tex as:

\begin{tikzpicture}[scale=.3]
\input{grid}[160][10][25]
%some other shapes
\end{tikzpicture}


or

\begin{tikzpicture}[scale=.3]
\input{grid}[300][15][15]
%some other shapes
\end{tikzpicture}


EDIT:

MWE as requested by @Andrew (working, but not ideal) -

main.tex:

\documentclass{exam}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
\input{grid}
\end{tikzpicture}
\end{center}
\end{document}


grid.tex:

\draw[black!20!white] (0,0) grid (20, 20);
\fill (0,0) circle (2pt) node[below left] {(0,200)};
\fill (20,0) circle (2pt) node[below right] {(200,200)};
\fill (0,20) circle (2pt) node[above left] {(0,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\fill (20,20) circle (2pt) node[above right] {(200,0)};
\tiny
\foreach \x in {20, 40, ..., 180} {
\fill (\x/10,20) circle (2pt) node[above] {\x};
}
\foreach \y in {20, 40, ..., 180} {
\fill (0,20-\y/10) circle (2pt) node[left] {\y};
}


EDIT 2:

Taking suggestion given by @Andrew, I have managed to define variables in the main file and use the same in grid.tex

main.tex:

\documentclass{exam}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
\def\gridsize{300}
\def\xMarker{45}
\def\yMarker{50}
\input{grid}
\end{tikzpicture}
\end{center}
\end{document}


grid.tex:

\pgfmathsetmacro\size{\gridsize/10}
\draw[black!20!white] (0,0) grid (\size,\size);
\fill (0,0) circle (2pt) node[below left] {(0,\gridsize)};
\fill (\size,0) circle (2pt) node[below right] {(\gridsize,\gridsize)};
\fill (0,\size) circle (2pt) node[above left] {(0,0)};
\fill (\size,\size) circle (2pt) node[above right] {(\gridsize,0)};
\fill (\size,\size) circle (2pt) node[above right] {(\gridsize,0)};
\tiny
\foreach \x in {0,\xMarker, ..., \gridsize} {
\fill (\x/10,\size) circle (2pt) node[above] {\x};
}
\foreach \y in {0,\yMarker, ..., \gridsize} {
\fill (0,\size-\y/10) circle (2pt) node[left] {\y};
}


Still hoping I can just pass the values to the file somehow :)

I don't think I can create a command since I can't put it inside tikz.

• Welcome to TeX.SX! Can you please expand the code snippets that you have posted to a full minimal working example. It is much easier to help you you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. Ideally it should compile unless, of course, that is what you are asking about. – Andrew Jul 20 '17 at 1:33
• I am pretty sure that it is not possible to pass arguments to \input in this way because \input is not really a macro and, instead, it simply includes the specified file. What you could do is define macros, say \A, \B, ..., in main.tex and then use these macros as "parameters" in the included file. – Andrew Jul 20 '17 at 1:34
• Alternatively, you could use a macro other than \input i.e. define a new macro. But \input simply takes one argument. That is, it isn't that you haven't figured out how to use it with more parameters. There are no more parameters. And this is certainly not a macro you want to mess around redefining. – cfr Jul 20 '17 at 2:14
• May I ask why you want to do it by inputting another file within the tikzpicture? It seems an odd thing to have your heart set on. Why not: define a macro to produce the grid? (Define it in another file if you like. Or not.) Or, better, perhaps, use a pic? (Define it in another file if you like. A TiKZ library or not. Or not. As you wish.) – cfr Jul 20 '17 at 2:17
• @gauravgupta Sure. Even with \NewDocumentCommand. I'd go for a pic, but a macro is certainly an option. – cfr Jul 20 '17 at 2:20

Do you want something like this?

\documentclass{exam}
\usepackage{tikz,xparse}
\NewDocumentCommand \mygrid { O {300} O {45} O {50} }
{
\pgfmathsetmacro\size{#1/10}
\draw[black!20!white] (0,0) grid (\size,\size);
\fill (0,0) circle (2pt) node[below left] {(0,#1)};
\fill (\size,0) circle (2pt) node[below right] {(#1,#1)};
\fill (0,\size) circle (2pt) node[above left] {(0,0)};
\fill (\size,\size) circle (2pt) node[above right] {(#1,0)};
\fill (\size,\size) circle (2pt) node[above right] {(#1,0)};
\begin{scope}[font=\tiny]
\foreach \x in {0,#2, ..., #1} {
\fill (\x/10,\size) circle (2pt) node[above] {\x};
}
\foreach \y in {0,#3, ..., #1} {
\fill (0,\size-\y/10) circle (2pt) node[left] {\y};
}
\end{scope}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.3]
\mygrid[200][35][25]
\end{tikzpicture}
\begin{tikzpicture}[scale=.3]
\mygrid
\end{tikzpicture}
\end{center}
\end{document}


• Thanks @cfr! that is exactly what i wanted to do. funnily, when i tried it it didn't work (i didn't use scope - maybe that's why). Thanks a Tonne :) – gaurav gupta Jul 20 '17 at 2:45
• @gauravgupta Did you get an error about a font due to the use of \tiny? tikzpicture sets all fonts to the 'null' font and then sets it back when typesetting the content of nodes. I'm not exactly sure of the specifics to explain why it is OK in the original code, but, when I got an error about font selection, I figured that was probably the culprit. – cfr Jul 20 '17 at 2:51
• font=\tiny it will handle OK. I just used a scope to limit it to the relevant part of the picture, really. If you took the \tiny out, it also solved the problem, but, obviously the node labels were a bit big then. – cfr Jul 20 '17 at 2:53