Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tikz.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_TIKZ_HH
2 # define VCSN_ALGOS_TIKZ_HH
3 
4 # include <algorithm>
5 # include <iostream>
6 # include <unordered_map>
7 # include <unordered_set>
8 # include <vector>
9 
10 # include <vcsn/algos/grail.hh> // outputter
11 
12 # include <vcsn/dyn/fwd.hh>
13 
14 namespace vcsn
15 {
16 
17  /*--------------------------.
18  | tikz(automaton, stream). |
19  `--------------------------*/
20 
21  namespace detail
22  {
26  template <typename Aut>
27  class tikzer: public outputter<Aut>
28  {
29  public:
31  using typename super_t::automaton_t;
32  using typename super_t::state_t;
33  using typename super_t::transition_t;
34  using typename super_t::weight_t;
35 
36  using super_t::aut_;
38  using super_t::os_;
39  using super_t::ws_;
40 
41  using super_t::super_t;
42 
46  void print_finitial_(const std::string& kind, const weight_t& w)
47  {
48  if (!ws_.is_zero(w))
49  {
50  os_ << ',' << kind;
51  if (ws_.show_one() || !ws_.is_one(w))
52  {
53  os_ << ',' << kind << " text=$\\left\\langle ";
54  ws_.print(w, os_, "latex") << "\\right\\rangle$";
55  }
56  }
57  }
58 
59  void operator()()
60  {
61  os_ <<
62  "\\documentclass{standalone}\n"
63  " \\usepackage{tikz}\n"
64  " \\usetikzlibrary{arrows.meta, automata, bend,"
65  " positioning, shapes.misc}\n"
66  " \\tikzstyle{automaton}=[shorten >=1pt,"
67  " >={Stealth[bend,round]}, initial text=]\n"
68  " \\tikzstyle{accepting}=[accepting by arrow]\n"
69  "\n"
70  "\\begin{document}\n"
71  "\\begin{tikzpicture}[automaton, auto]\n"
72  ;
73 
74  state_t prev = aut_->null_state();
75  for (auto s : aut_->states())
76  {
77  os_ << " \\node[state";
78  print_finitial_("initial", aut_->get_initial_weight(s));
79  print_finitial_("accepting", aut_->get_final_weight(s));
80  if (aut_->state_has_name(s))
81  os_ << ",rounded rectangle";
82  os_ << "] (";
83  aut_->print_state(s, os_);
84  os_ << ')';
85  if (prev != aut_->null_state())
86  {
87  os_ << " [right=of ";
88  aut_->print_state(prev, os_);
89  os_ << ']';
90  }
91  os_ << " {$";
92  aut_->print_state_name(s, os_, "latex");
93  os_ << "$};\n";
94  prev = s;
95  }
96 
97  for (auto src : aut_->states())
98  {
99  std::set<state_t> ds;
100  for (auto t: aut_->out(src))
101  ds.insert(aut_->dst_of(t));
102  for (auto dst: ds)
103  {
104  os_ << " \\path[->] (";
105  aut_->print_state(src, os_);
106  os_ << ") edge"
107  << (src == dst ? "[loop above]" : "")
108  << " node"
109  << " {$";
110  print_entry_(src, dst, os_, "latex");
111  os_ << "$} (";
112  aut_->print_state(dst, os_);
113  os_ << ");\n";
114  }
115  }
116  os_ <<
117  "\\end{tikzpicture}\n"
118  "\\end{document}";
119  }
120  };
121  }
122 
126  template <typename AutPtr>
127  std::ostream&
128  tikz(const AutPtr& aut, std::ostream& out)
129  {
130  detail::tikzer<AutPtr> t{aut, out};
131  t();
132  return out;
133  }
134 
135  namespace dyn
136  {
137  namespace detail
138  {
140  template <typename Aut, typename Ostream>
141  std::ostream& tikz(const automaton& aut, std::ostream& out)
142  {
143  return tikz(aut->as<Aut>(), out);
144  }
145 
147  (const automaton& aut, std::ostream& out) -> std::ostream&);
148  }
149  }
150 }
151 
152 #endif // !VCSN_ALGOS_TIKZ_HH
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
std::ostream & print_entry_(state_t src, state_t dst, std::ostream &os, const std::string &fmt="text")
The labels and weights of transitions from src to dst.
Definition: grail.hh:73
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:147
weight_t_of< automaton_t > weight_t
Definition: grail.hh:47
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Print automaton to TikZ format.
Definition: tikz.hh:128
std::ostream & os_
Output stream.
Definition: grail.hh:143
transition_t_of< automaton_t > transition_t
Definition: grail.hh:45
Factor common bits in automaton formatting.
Definition: grail.hh:27
std::ostream & tikz(const automaton &aut, std::ostream &out)
Bridge.
Definition: tikz.hh:141
Format automaton to TikZ format.
Definition: tikz.hh:27
void operator()()
Definition: tikz.hh:59
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:141
state_t_of< automaton_t > state_t
Definition: grail.hh:40
void print_finitial_(const std::string &kind, const weight_t &w)
Format an initial/final weight.
Definition: tikz.hh:46