- MWE
- Default settings
- Useful troubleshootings
- General information
Last active
June 16, 2024 03:46
-
-
Save tapyu/7e16af891660d431b1c7bdbe91ff5566 to your computer and use it in GitHub Desktop.
General Tikz/PGFplots snippets
- In Tikz, some commands are designed to handle the necessary expansions internally. When they processes its coordinates, it internally expands the content appropriately. In contrast, other TikZ commands don't handle expansions the same way, and using
\edef
helps to ensure that the content is expanded at the right time. For instance,\addplot
: This command is designed to handle expansions internally. When you provide coordinates to \addplot, it knows how to process them, and you don't need to worry about explicit expansions.\coordinate
: This command, when used directly, might not handle expansions in the same way. Therefore, using\edef
and\noexpand
is a way to force the expansion at the right time.\noexpand
is used to protect the subsequent token from immediate expansion. Without\noexpand
,\coordinate
might get expanded at the wrong time.
\documentclass[beamer,crop]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach{1,2,3}{
\addplot[blue, mark=square, ycomb] coordinates {(#1, {#1^2 - 1})};
\edef\temp{\noexpand\coordinate (test#1) at (axis cs:#1,{#1^2 - 1});}
\temp
}
\end{axis}
Access the coordinates after the axis environment
\foreach \w in {1,2,3} {
\node at (test\w) {test\w};
}
\end{tikzpicture}
\end{document}
- The
\pgfplotsinvokeforeach
command is specifically designed for use within the pgfplots package and is often recommended for iterating over values when creating plots. It has some advantages over the more general\foreach
command from TikZ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[varwidth=true, border=2pt]{standalone} | |
% \documentclass[beamer,crop]{standalone} % for Beamer | |
\usepackage{tikz} | |
\usetikzlibrary{arrows,positioning} | |
\begin{document} | |
\begin{tikzpicture} | |
% Your Codes should be here | |
\end{tikzpicture} | |
\end{document} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\tikzset{ar/.style={-latex,shorten >=-1pt, shorten <=-1pt}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% see https://tex.stackexchange.com/a/51761/211183 | |
%%% | |
% tikzpic.tex | |
\documentclass[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview' | |
%\usetikzlibrary{...}% tikz package already loaded by 'tikz' option | |
\begin{document} | |
\begin{tikzpicture} | |
\draw (0,0) -- (10,10); % ... | |
\end{tikzpicture} | |
\end{document} | |
%%% | |
%%% | |
% main.tex | |
% compile with `pdflatex -shell-escape main` or `xelatex -shell-escape main` | |
\documentclass{article} | |
\usepackage[mode=buildnew]{standalone}% requires -shell-escape | |
\usepackage{tikz} | |
\begin{document} | |
Lorem ipsum ... | |
\begin{figure}[htbp] | |
\centering | |
\includestandalone[width=.8\textwidth]{tikzpic} | |
\caption{Distribution of the residuals.} | |
\label{fig:distribution} | |
\end{figure} | |
Lorem ipsum ... | |
\end{document} | |
%%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\pgfplotsset{ | |
compat=1.17, % Specify the compatibility version | |
nice_axis/.style={ | |
xlabel={$x$}, | |
ylabel={$y$}, | |
title={Sine Function}, | |
grid=both, | |
axis lines=middle, | |
samples=100, | |
domain=-2*pi:2*pi, | |
xmax=7, | |
xmin=-7, | |
ymax=1.5, | |
ymin=-1.5, | |
xtick={-2*pi, -3*pi/2, -pi, -pi/2, 0, pi/2, pi, 3*pi/2, 2*pi}, | |
xticklabels={$-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$, 0, $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$}, | |
ytick={-1, 0, 1}, | |
legend pos=outer north east, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\pgfmathdeclarefunction{sinc}{1}{% | |
\pgfmathparse{(#1==0 ? 1: sin(pi*#1 r))/(#1==0 ? 1: pi*#1)}% | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment