I'd like to generate a chord cheatsheet for my ukulele songbook. (make this in latex : )
But all I can find are packages made for guitar chords. They are too complex and not suitable for me.
Do you have any ideas how it could be done?
diagramsmusic
I'd like to generate a chord cheatsheet for my ukulele songbook. (make this in latex : )
But all I can find are packages made for guitar chords. They are too complex and not suitable for me.
Do you have any ideas how it could be done?
I think TikZ would be great for this, but you'll probably need to write a package for it. I experimented a little bit, and here is some basic functionality. (I used some code from Bell Curve/Gaussian Function/Normal Distribution in TikZ/PGF)
The code in the preamble defines a new command, \randomvar
, which can be used inside a tikzpicture
environment to define a random variable. In the main document code, you can see how this is used. One can specify the distribution, a variable name, etc. The code defines four random variables, which show up as TikZ nodes, and so drawing arrows from and to them is easy.
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
% --- this here would go into a package
\tikzset{bayes/pdf/.style={blue!50!white}}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\pgfmathdeclarefunction{exponential}{1}{%
\pgfmathparse{(#1) * exp(-(#1) * x)}%
}
\pgfkeys{/tikz/bayes/label/.initial={}}
\pgfkeys{/tikz/bayes/name/.initial={}}
\pgfkeys{/tikz/bayes/distribution/.initial={0}}
\pgfkeys{/tikz/bayes/distribution name/.initial={}}
\tikzstyle{bayes/node}=[]
\newcommand\randomvar[2][1]{%
\begingroup
\pgfkeys{/tikz/bayes/.cd, #1}%
\pgfkeysgetvalue{/tikz/bayes/distribution}{\distribution}%
\pgfkeysgetvalue{/tikz/bayes/distribution name}{\distname}%
\pgfkeysgetvalue{/tikz/bayes/name}{\parname}%
\node[bayes/node] (#2) {
\tikz{
\begin{axis}[width=4cm, height=3cm,
axis x line=none,
axis y line=none, clip=false]
\addplot[blue!50!white, semithick, mark=none,
domain=-2:2, samples=50, smooth] {\distribution};
\addplot[black, yshift=-4pt] coordinates { (-2, 0) (2, 0) };
\node at (rel axis cs: 0.5, 0.5) {\parname};
\node[anchor=south] at (rel axis cs: 0.5, 0) {\sffamily\tiny\distname};
\end{axis}
}
};
\endgroup
}
% --- this here would be code written by the user
\begin{document}
\begin{tikzpicture}[node distance=3cm and 2cm, >=stealth]
\randomvar[distribution={gauss(0,0.5)},
name=$M_0$,
distribution name=normal]{M0}
\randomvar[distribution={gauss(0,0.5)},
distribution name=normal,
name=$M_1$,
node/.style={right of=M0}]{M1}
\node[below of=M1] (eqn) { $\beta_0 + \beta_1 \mathbf{x}_i$ };
\randomvar[distribution={exponential(3)},
distribution name=exponential,
name=$M_2$,
node/.style={right of=eqn}]{M2}
\randomvar[distribution={gauss(0,0.5)},
distribution name=normal,
node/.style={below of=eqn}]{M3}
\draw[->] (eqn) -- node [anchor=east] {$=$} (M3.center);
\draw[->] (M0.south) -- node [anchor=east] {$\sim$} (eqn.north west);
\draw[->] (M1.south) -- node [anchor=east] {$\sim$} (eqn);
\draw[->] (M2.south) -- node [anchor=east] {$\sim$} (M3);
\end{tikzpicture}
\end{document}
The output is:
This code could be a start for a package, but clearly a lot of functionality is missing*. For example, it should be possible to add parameters to the distributions (e.g. the tau of your normal distribution), and define anchors for these parameters to allow for drawing arrows to them (notice that the exact positioning of the anchors would have to depend on the distribution to look good). I think it is possible to add more anchors like .south west
; so one could refer to the first parameter of node M3
as M3.parameter 1
or something. Then it would be possible to, say, draw an arrow from M1 to the parameter of M3 by writing \draw[->] (M1.south) -- (M3.parameter 1);
Another issue is drawing arrows to the parameters in equations (in the equation containing the beta's). I don't immediately see how to do that right now, but I'm no TikZ expert.
In conclusion, although it may take some work and expertise to develop this (as expected), I do think that a TikZ package would be able to automate a good deal of the work of drawing these diagrams.
*) I also don't know if I use the right coding conventions regarding e.g. pgfkeys -- comments welcomed.
I figured out enough about tikz to produce the following:
My code is below. Comments would be appreciated, since I don't really know what I'm doing with tikz. Is there a good way to avoid hardcoding the distances between nodes and the size of the circle?
\documentclass{article}
\RequirePackage{tikz}
\begin{document}
\pgfdeclarelayer{nodelayer}
\pgfdeclarelayer{edgelayer}
\pgfsetlayers{edgelayer,nodelayer,main}
\tikzstyle{plain}=[rectangle,fill=none,draw=black,scale=1.0,inner sep=1.8pt] % circle also works
\tikzstyle{arrow}=[draw=black,arrows=-latex]
The notation
\begin{tikzpicture}[baseline=(n0.base),thick]
\begin{pgfonlayer}{nodelayer}
\node [style=plain] (n0) at (0, 0) {$u$};
\node [style=plain] (n1) at +(2.3em, 0) {$v$};
\end{pgfonlayer}
\begin{pgfonlayer}{edgelayer}
\draw [style=arrow] (n0) to (n1);
\end{pgfonlayer}
\end{tikzpicture}
means the inner product $u_a v^a$.
The following displayed picture represents the tensor expression $u^a_{abc}\nabla_d v^{bc}$:
\begin{tikzpicture}[thick]
\begin{pgfonlayer}{nodelayer}
\node [style=plain] (n0) at (0, 0) {$u$};
\node [style=plain] (n1) at (3.1em, 0) {$v$};
\node [style=circle,draw=black,minimum size=2.1\baselineskip] (c1) at (n1) {};
\end{pgfonlayer}
\begin{pgfonlayer}{edgelayer}
\draw [style=arrow,out=10,in=170] (n0) to (n1);
\draw [style=arrow,out=-10,in=190] (n0) to (n1);
\draw [style=arrow,out=30,in=150, distance=1.8\baselineskip, loop] (n0) to ();
\draw [style=arrow] (c1.east) to +(1.4em,0);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Best Answer
Edit: Here is a better
\drawukulelechord
macro using TikZ.The
\drawukulelechord
macro draws a ukulele chord. Its mandatory argument is a list of four numbers (for four strings) : -1 (string is not played), 0 (opened string), 1 and above (finger position for this string). The optional argument may be used to change scale (default:x=2ex
andy=2ex
).The
\defineukulelechord
macro allows to associate a name (first argument) and a list to use with\drawukulelechord
(second argument).The
\ukulelechord
macro draws a named ukulele chord (predefined by\defineukulelechord
).You may uncomment or comment the line with
% opened string
to explicitly draw or not an opened string.