%20y%20hacer%20c%C3%A1lculos%20con%20ellos%3F.png)
Soy nuevo en la creación de comandos en LaTeX y me gustaría hacer algo como agregar un nombre más útil dentro de mi comando, mientras hago cálculos con él; mientras también hace algunos cálculos con dicho parámetro. He aquí un ejemplo:
\documentclass{article}
\usepackage{tikz}
\newcommand{\gogrid}[1][1]{
% Here are some of my attempts:
% \def\size{#1 / 19}
\newlength{\size}
\setlength{\size}{#1 / 19}
% Maybe I need to specify the type of the `#1` parameter?
\draw[step=#1 / 19] (-#1, -#1) grid (#1,#1);
\draw[line width=1] (-#1, #1) -- (#1 + 0.015, #1); % chktex 8
\draw[line width=1] (#1, -#1) -- (#1, #1); % chktex 8
}
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\gogrid
% \draw(-5,-5) grid (5,5);
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\caption{\label{my_grid}A grid with two points!}
\end{center}
\end{figure}
\end{document}
¿Es esto posible en LaTeX? Utilizo mucho este tipo de cosas en lenguajes de programación habituales para hacer las cosas más legibles. (Además, como nota al margen, si alguien tiene una referencia de un libro o un sitio web sobre este tipo de cosas, se lo agradecería mucho).
Respuesta1
Todo funcionará en su ejemplo, si pudiera definir una unidad adecuada para el argumento #1
en el \setlength
comando, mientras usa también la sintaxis adecuada para el cálculo de dimensiones dentro de TikZ. Podrías omitir las unidades para el dibujo de TikZ pero no para el cálculo de dimensiones. Entonces debes usar \setlength{\size}{\dimexpr#1cm/19\relax}
.
En TikZ, si se omite la unidad de las coordenadas, cm
se asumen. Para el ancho de línea, pt
se utilizará.
Como se mencionó en los comentarios, \newlength{\size}
debe colocarse fuera de la definición del comando, porque no es necesario redefinir la misma longitud nueva cada vez.
Aquí hay dos ejemplos que utilizan su código para generar la cuadrícula. Imprimí el valor de \size
como un nodo debajo del punto central y establecí el radio del punto central igual a la \size
longitud, solo para mostrar que los cálculos de dimensiones realmente funcionaron. Los puntos centrales ocupan exactamente 4 bloques.
\documentclass{article}
\usepackage{tikz}
\newlength{\size}
\newcommand{\gogrid}[1][1]{
\setlength{\size}{\dimexpr#1cm/19\relax}
\draw[step=\size] (-#1, -#1) grid (#1,#1);
\draw[red,line width=1] (-#1, #1) -- (\dimexpr#1cm+1cm , #1);
\draw[blue,line width=1] (#1, -#1) -- (#1, #1);
}
\begin{document}
\begin{figure}[ht]
\begin{center}
\begin{tikzpicture}
\gogrid
\filldraw (0,0) circle [radius=1.49751pt] node[below] {\the\size};
\filldraw (1,1) circle[radius=1.49751pt];
\end{tikzpicture}
\caption{A grid with two points!}\label{my_grid1}
\end{center}
\end{figure}
\begin{figure}[ht]
\begin{center}
\begin{tikzpicture}
\gogrid[5]
\filldraw (0,0) circle [radius=7.48756pt] node[below=0.5cm] {\the\size};
\filldraw (5,5) circle[radius=3pt];
\end{tikzpicture}
\caption{Another grid with two points!}\label{my_grid2}
\end{center}
\end{figure}
\end{document}
Producción:
Respuesta2
Si está haciendo esto dentro de tikzpicture
s, puede dejar que pgfmath
se encargue de los cálculos. Simplemente eliminar las líneas \newlength...
y \setlength...
funciona.
\newcommand{\gogrid}[1][1]{%
\draw[step=#1 / 19] (-#1, -#1) grid (#1,#1);
\draw[line width=1] (-#1, #1) -- (#1 + 0.015, #1); % chktex 8
\draw[line width=1] (#1, -#1) -- (#1, #1); % chktex 8
}
habilita \gogrid[<number or length>]
el interior tikzpicture
s.
\begin{tikzpicture}
\gogrid[10mm]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\begin{tikzpicture}
\gogrid[5]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
Sin embargo, para este uso específico, puedes usarlo grid
directamente.
\begin{tikzpicture}
\draw [step=2/19] (-2,-2) grid (2,2);
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
Tenga en cuenta que si no especifica unidades, PGF utilizará la unidad actual en la mayoría de los casos. De forma predeterminada, es 1 cm en cada eje, pero el valor es personalizable y se puede configurar de forma independiente para horizontal y vertical, si es necesario.
Código completo:
\documentclass{standalone}
\usepackage{tikz}
\newcommand{\gogrid}[1][1]{%
\draw[step=#1 / 19] (-#1, -#1) grid (#1,#1);
\draw[line width=1] (-#1, #1) -- (#1 + 0.015, #1); % chktex 8
\draw[line width=1] (#1, -#1) -- (#1, #1); % chktex 8
}
\begin{document}
\begin{tikzpicture}
\gogrid[10mm]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\begin{tikzpicture}
\gogrid[5]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\begin{tikzpicture}
\draw [step=2/19] (-2,-2) grid (2,2);
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\end{document}
EDITAR
Si desea utilizar una macro en lugar de un parámetro en los cálculos para mayor claridad, la usaría pgfkeys
si lo hace en un archivo tikzpicture
. Hay varias formas de configurar esto pero, si solo quieres especificar el tamaño (y no quieres modificar la cuadrícula con ninguna otra opción), intentaría algo como
\documentclass{standalone}
\usepackage{tikz}
\tikzset{%
go grid/.code={%
\tikzset{%
go grid/.cd,
size=#1,
}%
\draw [step=\gogridsize/19] (-\gogridsize, -\gogridsize) grid (\gogridsize,\gogridsize);
\draw[line width=1] (-\gogridsize, \gogridsize) -- (\gogridsize + 0.015, \gogridsize); % chktex 8
\draw[line width=1] (\gogridsize, -\gogridsize) -- (\gogridsize, \gogridsize); % chktex 8
},
go grid/.default=10mm,
go grid/.cd,
size/.store in=\gogridsize,
size/.initial=1cm,
}
\begin{document}
\begin{tikzpicture}[go grid]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\begin{tikzpicture}[go grid=5]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\begin{tikzpicture}[go grid=20mm]
\filldraw[black](0,0)circle[radius=3pt];
\filldraw[black](1,1)circle[radius=3pt];
\end{tikzpicture}
\end{document}
Si desea varias cuadrículas por imagen, puede usar pic
o, como lo hizo inicialmente, simplemente definir un comando.
\newcommand{\gogrid}[1][1]{%
\tikzset{go grid/size=#1,go grid}%
}
Si desea especificar otros aspectos de las cuadrículas, nuevamente existen diferentes enfoques. Probablemente probaría algo como
\documentclass{standalone}
\usepackage{tikz}
\tikzset{% ateb: https://tex.stackexchange.com/a/708415/ addaswyd o gwestiwn: https://tex.stackexchange.com/q/708410/
go grid/.search also={/tikz,/pgf},
go grid/.code={%
\tikzset{%
go grid/.cd,
#1,
}%
\begin{scope}[draw=\gogridcolour]
\draw [step=\gogridsize/19] (-\gogridsize, -\gogridsize) grid (\gogridsize,\gogridsize);
\draw[line width=1] (-\gogridsize, \gogridsize) -- (\gogridsize + 0.015, \gogridsize); % chktex 8
\draw[line width=1] (\gogridsize, -\gogridsize) -- (\gogridsize, \gogridsize); % chktex 8
\end{scope}
\edef\tempa{}\edef\tempb{\gogridstonesat}%
\ifx\tempa\tempb
\else
\foreach \i in \gogridstonesat \filldraw[\gogridstones]\i circle [radius=\gogridstonesize];
\fi
},
go grid/.default={size=10mm},
go grid/.cd,
grid/.store in=\gogridcolour,
grid=black,
size/.store in=\gogridsize,
size=1cm,
stone size/.store in=\gogridstonesize,
stone size=3pt,
stones/.store in=\gogridstones,
stones=black,
stones at/.store in=\gogridstonesat,
stones at={},
}
\NewDocumentCommand \gogrid { O{} }
{%
\tikz[go grid={#1}]{}%
}
\begin{document}
\begin{tikzpicture}[go grid={grid=blue,stones at={(0,0),(1,1)},stones=red}]
\end{tikzpicture}
\begin{tikzpicture}[go grid={size=5,stones at={(0,0),(1,1)}}]
\end{tikzpicture}
\begin{tikzpicture}[go grid={size=20mm,grid=green,stones at={(0,0),(1,1)},stone size=4pt}]
\node [above] at (current bounding box.north) {Green Go Grid};
\end{tikzpicture}
\gogrid
\gogrid[size=2.5cm,grid=orange,stones at={(0,1),(1,0),(-1,0),(0,-1)},stone size=2.5pt,stones=blue]
\end{document}