Instantly share code, notes, and snippets.

Embed
What would you like to do?
ERD LateX doodles
\draw [one to one] (0,0) -- (12,0) node[midway, above] {can't work out the layout} ;
\draw [one to one] (0,2) -- (5,2) node[midway, above] {one to one} ;
\draw [many to many] (7,2) -- (12,2) node[midway, above] {many to many} ;
\draw [many to one] (0,4) -- (5,4) node[midway, above] {many to one} ;
\draw [one to many] (7,4) -- (12,4) node[midway, above] {one to many} ;
\draw [many to oone] (0,6) -- (5,6) node[midway, above] {many to oone} ;
\draw [oone to many] (7,6) -- (12,6) node[midway, above] {oone to many} ;
\draw [many to omany] (0,8) -- (5,8) node[midway, above] {many to omany} ;
\draw [omany to many] (7,8) -- (12,8) node[midway, above] {omany to many} ;
\draw [omany to none] (0,10) -- (5,10) node[midway, above] {omany to none} ;
\draw [none to omany] (7,10) -- (12,10) node[midway, above] {none to omany} ;
\draw [omany to one] (0,12) -- (5,12) node[midway, above] {omany to one} ;
\draw [one to omany] (7,12) -- (12,12) node[midway, above] {one to omany} ;
\draw [omany to oone] (0,14) -- (5,14) node[midway, above] {omany to oone} ;
\draw [oone to omany] (7,14) -- (12,14) node[midway, above] {oone to omany} ;
\draw [one to none] (0,16) -- (5,16) node[midway, above] {one to none} ;
\draw [none to one] (7,16) -- (12,16) node[midway, above] {none to one} ;
\draw [oone to one] (0,18) -- (5,18) node[midway, above] {oone to one} ;
\draw [one to oone] (7,18) -- (12,18) node[midway, above] {one to oone} ;
\draw [oone to none] (0,20) -- (5,20) node[midway, above] {oone to none} ;
\draw [none to oone] (7,20) -- (12,20) node[midway, above] {none to oone} ;
latex='''
%via https://www.reddit.com/r/LaTeX/comments/2pwu0g/made_a_diagram_in_tikz_not_sure_how_to_improve/
%via https://tex.stackexchange.com/questions/393560/tikz-crows-foot-notation-on-only-one-side-for-documentation
\documentclass{{standalone}}
\usepackage{{tikz}}
\usetikzlibrary{{shapes.multipart}}
\usetikzlibrary{{matrix}}
\usetikzlibrary{{positioning}}
\usetikzlibrary{{shadows}}
\usetikzlibrary{{calc}}
\usetikzlibrary{{%
chains,
}}
\tikzset{{
entity/.code={{
\tikzset{{
%label=above:#1,
name=#1,
inner sep=0pt,
every entity/.try,
fill=white,
%general shadow={{
% shadow xshift=0.0625in,
% shadow yshift=-0.0625in,
% opacity=0.2,
% fill=black!50
%}}
}}%
\def\entityname{{#1}}%
}},
entity anchor/.style={{matrix anchor=#1.center}},
every entity/.style={{
draw,
}},
every property/.style={{
% inner xsep=0.25cm (the align center is for just the single box' define a new element for that?
align=center, inner ysep=0.525cm, anchor=west, text width=1.5in
}},
dummy/.style={{
rectangle,
rounded corners,
draw=white, very thick,
text centered}},
zig zag to/.style={{
to path={{(\tikztostart) -| ($(\tikztostart)!#1!(\tikztotarget)$) |- (\tikztotarget) \tikztonodes}}
}},
zig zag to/.default=0.5,
one to one/.style={{
one-one, zig zag to
}},
one to none/.style={{
one-, zig zag to
}},
none to one/.style={{
-one, zig zag to
}},
none to oone/.style={{
-oone, zig zag to
}},
oone to one/.style={{
oone-one, zig zag to
}},
one to oone/.style={{
one-oone, zig zag to
}},
none to omany/.style={{
-omany, zig zag to
}},
oone to none/.style={{
oone-, zig zag to
}},
omany to none/.style={{
omany-, zig zag to
}},
omany to one/.style={{
omany-one, zig zag to
}},
one to omany/.style={{
one-omany, zig zag to
}},
one to many/.style={{
one-crow's foot, zig zag to,
}},
many to one/.style={{
crow's foot-one, zig zag to
}},
oone to many/.style={{
oone-crow's foot, zig zag to,
}},
many to oone/.style={{
crow's foot-oone, zig zag to
}},
many to omany/.style={{
crow's foot-omany, zig zag to
}},
omany to many/.style={{
omany-crow's foot, zig zag to
}},
many to many/.style={{
crow's foot-crow's foot, zig zag to
}},
oone to omany/.style={{
oone-crow's foot, zig zag to
}},
omany to oone/.style={{
crow's foot-oone, zig zag to
}},
}}
\makeatletter
\pgfarrowsdeclare{{crow's foot}}{{crow's foot}}
{{
\pgfarrowsleftextend{{+-.5\pgflinewidth}}%
\pgfarrowsrightextend{{+.5\pgflinewidth}}%
}}
{{
\pgfutil@tempdima=0.6pt%
%\advance\pgfutil@tempdima by.25\pgflinewidth%
\pgfsetdash{{}}{{+0pt}}%
\pgfsetmiterjoin%
\pgfpathmoveto{{\pgfqpoint{{0pt}}{{-9\pgfutil@tempdima}}}}%
\pgfpathlineto{{\pgfqpoint{{-13\pgfutil@tempdima}}{{0pt}}}}%
\pgfpathlineto{{\pgfqpoint{{0pt}}{{9\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{-8pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-8pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-8pt}}{{6pt}}}}%
\pgfusepathqstroke%
}}
\pgfarrowsdeclare{{omany}}{{omany}}
{{
\pgfarrowsleftextend{{+-.5\pgflinewidth}}%
\pgfarrowsrightextend{{+.5\pgflinewidth}}%
}}
{{
\pgfutil@tempdima=0.6pt%
%\advance\pgfutil@tempdima by.25\pgflinewidth%
\pgfsetdash{{}}{{+0pt}}%
\pgfsetmiterjoin%
\pgfpathmoveto{{\pgfqpoint{{0pt}}{{-9\pgfutil@tempdima}}}}%
\pgfpathlineto{{\pgfqpoint{{-13\pgfutil@tempdima}}{{0pt}}}}%
\pgfpathlineto{{\pgfqpoint{{0pt}}{{9\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{-6pt}}{{-6pt}}}}%
\pgfpathcircle{{\pgfpoint{{-11.5pt}}{{0}}}} {{3.5pt}}
\pgfusepathqstroke%
}}
\pgfarrowsdeclare{{oone}}{{oone}}
{{
\pgfarrowsleftextend{{+-.5\pgflinewidth}}%
\pgfarrowsrightextend{{+.5\pgflinewidth}}%
}}
{{
\pgfutil@tempdima=0.6pt%
%\advance\pgfutil@tempdima by.25\pgflinewidth%
\pgfsetdash{{}}{{+0pt}}%
\pgfsetmiterjoin%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{-6pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-6pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-6pt}}{{6pt}}}}%
\pgfpathcircle{{\pgfpoint{{-11.5pt}}{{0}}}} {{3.5pt}}
\pgfusepathqstroke%
}}
\pgfarrowsdeclare{{one}}{{one}}
{{
\pgfarrowsleftextend{{+-.5\pgflinewidth}}%
\pgfarrowsrightextend{{+.5\pgflinewidth}}%
}}
{{
\pgfutil@tempdima=0.6pt%
%\advance\pgfutil@tempdima by.25\pgflinewidth%
\pgfsetdash{{}}{{+0pt}}%
\pgfsetmiterjoin%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{-6pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-6pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-6pt}}{{6pt}}}}%
\pgfpathmoveto{{\pgfqpoint{{0\pgfutil@tempdima}}{{0\pgfutil@tempdima}}}}%
\pgfpathmoveto{{\pgfqpoint{{-8pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-8pt}}{{-6pt}}}}%
\pgfpathlineto{{\pgfqpoint{{-8pt}}{{6pt}}}}%
\pgfusepathqstroke%
}}
\def\property#1{{\node[name=\entityname-#1, every property/.try]{{#1}};}}
\def\properties{{\begingroup\catcode`\_=11\relax\processproperties}}
\def\processproperties#1{{\endgroup%
\def\propertycode{{}}%
\foreach \p in {{#1}}{{%
\expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\propertycode%
\expandafter\expandafter\expandafter{{\expandafter\propertycode\expandafter\property\expandafter{{\p}}\\}}%
}}%
\propertycode%
}}
%The following is for handling foreign and primary keys
\def\pk#1{{\node[name=\entityname-#1, every property/.try]{{#1}};
\node[name=\entityname-#1, every property/.try, red, text width=1in, align=right,]{{(PK)}};
\\}}
\def\fk#1{{\node[name=\entityname-#1, every property/.try]{{#1}};
\node[name=\entityname-#1, every property/.try, red, text width=1in, align=right]{{(FK)}};
\\}}
\begin{{document}}
\begin{{tikzpicture}}
{diag}
\end{{tikzpicture}}
\end{{document}}
'''.format(diag=diag)
diag='''
\matrix [entity=Customer, entity anchor=Customer-Customer] {
\properties{
Customer
}
};
\matrix [entity=DiskOrder, below=3cm of Customer-Customer, entity anchor=DiskOrder-DiskOrder] {
\properties{
DiskOrder
}
};
\matrix [entity=OrderItem, below=3cm of DiskOrder-DiskOrder, entity anchor=OrderItem-OrderItem] {
\properties{
OrderItem
}
};
\draw [one to omany] (Customer-Customer) -- (DiskOrder-DiskOrder) ;
\draw [one to many] (DiskOrder-DiskOrder) -- (OrderItem-OrderItem) ;
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment