Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Add arrows for TikZ diagrams #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Text/LaTeX/Packages/TikZ/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Point = (Double,Double)
-- | A figure in the plane.
data Figure =
Line [Point] -- ^ Line along a list of points.
| Arrow [Point] -- ^ Line along a list of points with an arrowhead .
| Curvy [Point]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You explain Arrow, but not Curvy.

| Polygon [Point] -- ^ Line along a list of points, but the last point will be joined
-- with the first one.
| PolygonFilled [Point] -- ^ Same as 'Polygon', but the inner side will be filled with color.
Expand All @@ -79,10 +81,18 @@ castpoint (x,y) = T.pointAtXY x y
radiansToDegrees :: Double -> Double
radiansToDegrees x = (180 * x) / pi

toTPoint :: Point -> T.TPoint
toTPoint = uncurry T.pointAtXY
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as castpoint.


-- | Translate a 'Figure' to a 'TikZ' script.
figuretikz :: Figure -> TikZ
figuretikz (Line []) = emptytikz
figuretikz (Line (p:ps)) = T.draw $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps
figuretikz (Arrow []) = emptytikz
figuretikz (Arrow (p:ps)) = T.path [T.Draw, T.Arrow] $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps
figuretikz (Curvy []) = emptytikz
--figuretikz (Curvy (p:ps)) = T.path [T.Draw, T.Smooth] $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps
figuretikz (Curvy (ps)) = T.Curvy (toTPoint <$> ps)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parenthesis in (ps) are not needed.

figuretikz (Polygon []) = emptytikz
figuretikz (Polygon (p:ps)) = T.draw $ T.Cycle $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps
figuretikz (PolygonFilled []) = emptytikz
Expand Down
11 changes: 9 additions & 2 deletions Text/LaTeX/Packages/TikZ/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Text.LaTeX.Packages.TikZ.Syntax (
, Color (..)
, Word8
-- * TikZ
, TikZ
, TikZ (..)
, emptytikz
, path
, scope
Expand Down Expand Up @@ -245,28 +245,35 @@ instance Render Parameter where
-- | A Ti/k/Z script.
data TikZ =
PathAction [ActionType] TPath
| Curvy [TPoint]
| Scope [Parameter] TikZ
| TikZSeq (S.Seq TikZ)
deriving Show

-- | Different types of actions that can be performed
-- with a 'TPath'. See 'path' for more information.
data ActionType = Draw | Fill | Clip | Shade deriving Show
data ActionType = Draw | Fill | Clip | Shade | Arrow deriving Show

-- | Just an empty script.
emptytikz :: TikZ
emptytikz = TikZSeq mempty

instance Render TikZ where
render (PathAction ts p) = "\\path" <> render ts <> " " <> render p <> " ; "
render (Curvy ps) = "\\draw[->] plot [smooth] coordinates { " <> (renderPoints ps) <> " } ; "
render (Scope ps t) = "\\begin{scope}" <> render ps <> render t <> "\\end{scope}"
render (TikZSeq ts) = foldMap render ts

renderPoints :: [TPoint] -> Text
renderPoints [] = ""
renderPoints (p:ps) = render p <> " " <> renderPoints ps

instance Render ActionType where
render Draw = "draw"
render Fill = "fill"
render Clip = "clip"
render Shade = "shade"
render Arrow = "->"

-- | A path can be used in different ways.
--
Expand Down