- MWE
- Default settings
- Useful troubleshootings
- General information
Last active
October 6, 2025 20:38
-
-
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
\edefhelps 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\edefand\noexpandis a way to force the expansion at the right time.\noexpandis used to protect the subsequent token from immediate expansion. Without\noexpand,\coordinatemight 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
\pgfplotsinvokeforeachcommand 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\foreachcommand from TikZ.
This file contains hidden or 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 hidden or 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 hidden or 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 | |
| \usepackage{pgfplots} | |
| \pgfplotsset{compat=newest} | |
| \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{tikzpic} | |
| \caption{Distribution of the residuals.} | |
| \label{fig:distribution} | |
| \end{figure} | |
| Lorem ipsum ... | |
| \end{document} | |
| %%% |
This file contains hidden or 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[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview' | |
| \usepackage{tikz} | |
| \usepackage{pgfplots} % loads tikz which loads pgf | |
| \usepackage{xparse} | |
| \pgfplotsset{compat=1.18} | |
| \begin{document} | |
| \begin{tikzpicture} | |
| \begin{axis}[ | |
| width=0.9\textwidth, | |
| height=0.45\textwidth, | |
| xlabel={Sample}, | |
| ylabel={Amplitude}, | |
| title={satellite1 L1 amplitude}, | |
| grid=both, | |
| grid style={line width=.1pt, draw=gray!30}, | |
| major grid style={line width=.2pt, draw=gray!70}, | |
| tick align=outside, | |
| tick style={black} | |
| ] | |
| \addplot+[ | |
| semithick, | |
| color=blue | |
| ] table [x=index, y=amplitude, col sep=space] {satellite1_L1_amplitude.dat}; | |
| \end{axis} | |
| \end{tikzpicture} | |
| \end{document} |
This file contains hidden or 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, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment