I have to draw the fallowing figure in LaTex but I don not know the code.
[Tex/LaTex] Lattice diagrams
diagramstikz-pgf
Related Solutions
The crows foot can be done quite simply with an arrow head. The rest is a matter of simplification using styles
and macros, which can be done in many ways. Here is one:
\documentclass[border=0.25in]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{shadows}
\usetikzlibrary{calc}
\makeatletter
\pgfarrowsdeclare{crow's foot}{crow's foot}
{
\pgfarrowsleftextend{+-.5\pgflinewidth}%
\pgfarrowsrightextend{+.5\pgflinewidth}%
}
{
\pgfutil@tempdima=0.5pt%
\advance\pgfutil@tempdima by.25\pgflinewidth%
\pgfsetdash{}{+0pt}%
\pgfsetmiterjoin%
\pgfpathmoveto{\pgfqpoint{0pt}{-6\pgfutil@tempdima}}%
\pgfpathlineto{\pgfqpoint{-6\pgfutil@tempdima}{0pt}}%
\pgfpathlineto{\pgfqpoint{0pt}{6\pgfutil@tempdima}}%
\pgfusepathqstroke%
}
\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.5,
fill=black!50
}
}%
\def\entityname{#1}%
},
entity anchor/.style={matrix anchor=#1.center},
every entity/.style={
draw,
},
every property/.style={
inner xsep=0.25cm, inner ysep=0.125cm, anchor=west, text width=1in
},
zig zag to/.style={
to path={(\tikztostart) -| ($(\tikztostart)!#1!(\tikztotarget)$) |- (\tikztotarget)}
},
zig zag to/.default=0.5,
one to many/.style={
-crow's foot, zig zag to
},
many to one/.style={
crow's foot-, zig zag to
},
many to many/.style={
crow's foot-crow's foot, zig zag to
}
}
\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%
}
\begin{document}
\begin{tikzpicture}[every node/.style={font=\ttfamily}, node distance=1.25in]
\matrix [entity=Class] {
\properties{
classkey,
class_type,
class_desc
}
};
\matrix [entity=Product, right=of Class-classkey, entity anchor=Product-classkey] {
\properties{
classkey,
prodkey,
prod_name,
pkg_type
}
};
\matrix [entity=Sales, right=of Product-classkey, entity anchor=Sales-classkey] {
\properties{
perkey,
classkey,
prodkey,
prod_name,
storekey,
promokey,
quantity,
dollars
}
};
\matrix [entity=Period, right=of Sales-perkey, entity anchor=Period-perkey] {
\properties{
perkey,
date,
day,
week,
month,
qtr,
year
}
};
\matrix [entity=Promotion, below=of Period, entity anchor=Promotion-promokey] {
\properties{
promokey,
promo_type,
promo_desc,
value,
start_date,
end_date
}
};
\matrix [entity=Store, below=of Product, entity anchor=Store-storekey] {
\properties{
storekey,
mktkey,
store_type,
store_name,
street,
city,
state,
zip
}
};
\matrix [entity=Market, left=of Store-mktkey, entity anchor=Market-mktkey] {
\properties{
mktkey,
hq_city,
hq_state,
district,
region
}
};
\draw [one to many] (Class-classkey) to (Product-classkey);
\draw [one to many] (Product-classkey) to (Sales-classkey);
\draw [one to many] (Product-prodkey) to (Sales-prodkey);
\draw [many to one] (Sales-perkey) to (Period-perkey);
\draw [one to many] (Market-mktkey) to (Store-mktkey);
\draw [many to one] (Sales-storekey) to (Store-storekey);
\draw [many to one] (Sales-promokey) to (Promotion-promokey);
\end{tikzpicture}
\end{document}
Here's a quick demonstration with TikZ, partly based on Thruston's Metapost answer.
Starting with the squares, the simples way of making those is TikZ' rectangle
operation, which is used as
\draw (x1,y1) rectangle (x2,y2);
So simply specify the coordinates of two opposite corners, with the word rectangle
between.
For the other shapes, you can draw the lines segment by segment, e.g.
\draw (0,0) -- (0,1) -- (1,1);
which draws a line from (0,0) to (0,1) to (1,1).
To add the dots at the end of the line, you can use an arrow tip from the arrows.meta
library. Hence, you need to add \usetikzlibrary{arrows.meta}
to the preamble. Arrow tips are specified in the optional argument to a \draw
command, generally as \draw [<name of arrow tip>-<name of arrow tip>] ...;
. Arrow tips can be added to one or both ends of a path.
So to add a black dot at the ends of the line, use
\draw [{Circle[]}-{Circle[]}] (0,0) -- (0,1);
Circle[]
is the name of the arrow tip. The reason for the brace pair ({}
) around it is that you need to "hide" the brackets from the parser, so that the closing bracket (]
) of the arrow tip isn't confused with the closing bracket for the optional argument(s) to \draw
.
The arc can be drawn in several ways. In the code below I've used the syntax (x1,y1) .. controls (x2,y2) and (x3,y3) .. (x4,y4)
. This draws a curved line between (x1,y1)
and (x4,y4)
. I can't explain how this works better than the first tutorial in TikZ' manual, so I'm quoting that, section 2.4:
For this, TikZ provides a special syntax. One or two “control points” are needed. The math behind them is not quite trivial, but here is the basic idea: Suppose you are at point
x
and the first control point isy
. Then the curve will start “going in the direction ofy
atx
,” that is, the tangent of the curve atx
will point towardy
. Next, suppose the curve should end atz
and the second support point isw
. Then the curve will, indeed, end atz
and the tangent of the curve at pointz
will go throughw
.
The control points I've used are one unit to the right of the start and end point.
There are two other bits of syntax used in the code below that I added because they could be useful. The first is the scope
environment, which allows you to add settings to all commands within. Here I've used
\begin{scope}[xshift=3cm]
...
\end{scope}
Unsurprisingly, what this will do is to move the coordinates of the paths within it 3cm to the right. (Could at this point mention that the default unit length is 1cm, so \draw (0,0) -- (0,1);
will draw a line that is 1cm long.)
The other bit is the use of relative coordinates. For example, if you write \draw (1,1) -- +(1,0) -- +(1,1);
this will draw a line from (1,1)
to (1+1,1+0) = (2,1)
to (1+1,1+1) = (2,2)
. With \draw (1,1) -- ++(1,0) -- ++(1,1);
(note two plus signs) this will draw a line from (1,1)
to (1+1,1+0) = (2,1)
to (2+1,1+1) = (3,2)
. In other words, with one plus sign, the previous explicit coordinate will always be the reference point, but with two plus signs, the reference coordinate will be moved.
That said, going through the tutorials in the TikZ manual (chapters 2-6), as mentioned in the comments by Tom Bombadil, would probably have learned you all of this and a lot more.
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\draw (0,1.5) rectangle +(1,1);
\draw [{Circle[]}-{Circle[]}] (2.5,0) -- (1.5,0) -- (1.5,1) -- (2.5,1);
\begin{scope}[xshift=3cm]
\draw [{Circle[]}-{Circle[]}] (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) --
++(0,1) -- ++(1,0) -- ++(0,1) -- ++(-1,0);
\draw [{Circle[]}-{Circle[]}] (2.5,0) -- (1.5,0) -- (1.5,1) --
(2.5,1) .. controls (3.5,1) and (3.5,3) .. (2.5,3) --
(1.5,3) -- (1.5,2) -- (2.5,2);
\end{scope}
\end{tikzpicture}
\end{document}
Best Answer
A simple approach can be done by Tikz. The result of the following is
The code for the preceding output is