From 14f4544a29eac182ff1f5840e5f9cc78b9efa7e0 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 4 Apr 2017 15:28:27 +0200
Subject: [PATCH] preprocessor: add option to write equation tags in latex
 output. closes #477

---
 doc/dynare.texi                | 10 ++++++++++
 preprocessor/ComputingTasks.cc |  7 ++++---
 preprocessor/ComputingTasks.hh |  3 ++-
 preprocessor/DynamicModel.cc   |  4 ++--
 preprocessor/DynamicModel.hh   |  4 ++--
 preprocessor/DynareBison.yy    |  6 ++++--
 preprocessor/DynareFlex.ll     |  1 +
 preprocessor/ModelTree.cc      | 13 ++++++++++++-
 preprocessor/ModelTree.hh      |  2 +-
 preprocessor/ParsingDriver.cc  |  4 ++--
 preprocessor/ParsingDriver.hh  |  2 +-
 11 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 9e980d681e..d355a76884 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -2097,6 +2097,7 @@ Compiling the @TeX{} file requires the following @LaTeX{} packages:
 @anchor{write_latex_dynamic_model}
 
 @deffn Command write_latex_dynamic_model ;
+@deffnx Command write_latex_dynamic_model (@var{OPTIONS}) ;
 
 @descriptionhead
 
@@ -2143,6 +2144,15 @@ also have been replaced by new auxiliary variables and equations.
 
 For the required @LaTeX{} packages, @pxref{write_latex_original_model}.
 
+@optionshead
+
+@table @code
+
+@item write_equation_tags
+Write the equation tags in the latex output
+
+@end table
+
 @end deffn
 
 @deffn Command write_latex_static_model ;
diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc
index 21b2dd7553..d441eca186 100644
--- a/preprocessor/ComputingTasks.cc
+++ b/preprocessor/ComputingTasks.cc
@@ -1617,15 +1617,16 @@ IdentificationStatement::writeOutput(ostream &output, const string &basename, bo
   output << "dynare_identification(options_ident);" << endl;
 }
 
-WriteLatexDynamicModelStatement::WriteLatexDynamicModelStatement(const DynamicModel &dynamic_model_arg) :
-  dynamic_model(dynamic_model_arg)
+WriteLatexDynamicModelStatement::WriteLatexDynamicModelStatement(const DynamicModel &dynamic_model_arg, bool write_equation_tags_arg) :
+  dynamic_model(dynamic_model_arg),
+  write_equation_tags(write_equation_tags_arg)
 {
 }
 
 void
 WriteLatexDynamicModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
-  dynamic_model.writeLatexFile(basename);
+  dynamic_model.writeLatexFile(basename, write_equation_tags);
 }
 
 WriteLatexStaticModelStatement::WriteLatexStaticModelStatement(const StaticModel &static_model_arg) :
diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh
index 596738c27c..8319bd893d 100644
--- a/preprocessor/ComputingTasks.hh
+++ b/preprocessor/ComputingTasks.hh
@@ -552,8 +552,9 @@ class WriteLatexDynamicModelStatement : public Statement
 {
 private:
   const DynamicModel &dynamic_model;
+  const bool write_equation_tags;
 public:
-  WriteLatexDynamicModelStatement(const DynamicModel &dynamic_model_arg);
+  WriteLatexDynamicModelStatement(const DynamicModel &dynamic_model_arg, bool write_equation_tags_arg);
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
 };
 
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index cc3dcf1c9b..212cec562e 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -4378,9 +4378,9 @@ DynamicModel::writeChainRuleDerivative(ostream &output, int eqr, int varr, int l
 }
 
 void
-DynamicModel::writeLatexFile(const string &basename) const
+DynamicModel::writeLatexFile(const string &basename, const bool write_equation_tags) const
 {
-  writeLatexModelFile(basename + "_dynamic", oLatexDynamicModel);
+  writeLatexModelFile(basename + "_dynamic", oLatexDynamicModel, write_equation_tags);
 }
 
 void
diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh
index 7f0177fbd0..7988a3658e 100644
--- a/preprocessor/DynamicModel.hh
+++ b/preprocessor/DynamicModel.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2016 Dynare Team
+ * Copyright (C) 2003-2017 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -278,7 +278,7 @@ public:
   size_t dynamicOnlyEquationsNbr() const;
 
   //! Writes LaTeX file with the equations of the dynamic model
-  void writeLatexFile(const string &basename) const;
+  void writeLatexFile(const string &basename, const bool write_equation_tags) const;
 
   //! Writes LaTeX file with the equations of the dynamic model (for the original model)
   void writeLatexOriginalFile(const string &basename) const;
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index c495801a03..7c918a1df1 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -110,7 +110,7 @@ class ParsingDriver;
 %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN
 %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO CONTEMPORANEOUS_CORRELATION DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL RAFTERY_LEWIS_QRS RAFTERY_LEWIS_DIAGNOSTICS MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION
 %token NUMBER_OF_PARTICLES RESAMPLING SYSTEMATIC GENERIC RESAMPLING_THRESHOLD RESAMPLING_METHOD KITAGAWA STRATIFIED SMOOTH
-%token CPF_WEIGHTS AMISANOTRISTANI MURRAYJONESPARSLOW
+%token CPF_WEIGHTS AMISANOTRISTANI MURRAYJONESPARSLOW WRITE_EQUATION_TAGS
 %token NONLINEAR_FILTER_INITIALIZATION FILTER_ALGORITHM PROPOSAL_APPROXIMATION CUBATURE UNSCENTED MONTECARLO DISTRIBUTION_APPROXIMATION
 %token <string_val> NAME
 %token USE_PENALIZED_OBJECTIVE_FOR_HESSIAN INIT_STATE
@@ -2111,7 +2111,9 @@ ramsey_policy_options : stoch_simul_primary_options
                       ;
 
 write_latex_dynamic_model : WRITE_LATEX_DYNAMIC_MODEL ';'
-                            { driver.write_latex_dynamic_model(); }
+                            { driver.write_latex_dynamic_model(false); }
+                          | WRITE_LATEX_DYNAMIC_MODEL '(' WRITE_EQUATION_TAGS ')' ';'
+                            { driver.write_latex_dynamic_model(true); }
                           ;
 
 write_latex_static_model : WRITE_LATEX_STATIC_MODEL ';'
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 0a1588c5ae..262d7a354c 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -465,6 +465,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2
   yylval->string_val = new string(yytext);
   return token::ABAND;
 }
+<DYNARE_STATEMENT>write_equation_tags {return token::WRITE_EQUATION_TAGS;}
 <DYNARE_STATEMENT>indxap {return token::INDXAP;}
 <DYNARE_STATEMENT>apband {return token::APBAND;}
 <DYNARE_STATEMENT>indximf {return token::INDXIMF;}
diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index 0b88c298f5..deec8b7238 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -1529,7 +1529,7 @@ ModelTree::Write_Inf_To_Bin_File(const string &basename,
 }
 
 void
-ModelTree::writeLatexModelFile(const string &basename, ExprNodeOutputType output_type) const
+ModelTree::writeLatexModelFile(const string &basename, ExprNodeOutputType output_type, const bool write_equation_tags) const
 {
   ofstream output, content_output;
   string filename = basename + ".tex";
@@ -1575,6 +1575,17 @@ ModelTree::writeLatexModelFile(const string &basename, ExprNodeOutputType output
     {
       content_output << "\\begin{dmath}" << endl
                      << "% Equation " << eq+1 << endl;
+      if (write_equation_tags)
+        for (vector<pair<int,pair<string,string> > >::const_iterator iteqt = equation_tags.begin();
+             iteqt != equation_tags.end(); iteqt++)
+          if (iteqt->first == eq)
+            {
+              content_output << "[\\textrm{" << iteqt->second.first << "}";
+              if (!empty(iteqt->second.second))
+                content_output << " = \\textrm{``" << iteqt->second.second << "''}";
+              content_output << "]";
+            }
+
       // Here it is necessary to cast to superclass ExprNode, otherwise the overloaded writeOutput() method is not found
       dynamic_cast<ExprNode *>(equations[eq])->writeOutput(content_output, output_type);
       content_output << endl << "\\end{dmath}" << endl;
diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh
index b87debf19c..1de07d4b35 100644
--- a/preprocessor/ModelTree.hh
+++ b/preprocessor/ModelTree.hh
@@ -196,7 +196,7 @@ protected:
   void compileModelEquations(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic) const;
 
   //! Writes LaTeX model file
-  void writeLatexModelFile(const string &basename, ExprNodeOutputType output_type) const;
+  void writeLatexModelFile(const string &basename, ExprNodeOutputType output_type, const bool write_equation_tags = false) const;
 
   //! Sparse matrix of double to store the values of the Jacobian
   /*! First index is equation number, second index is endogenous type specific ID */
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index c0f4729164..bce5408512 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -1963,9 +1963,9 @@ ParsingDriver::discretionary_policy()
 }
 
 void
-ParsingDriver::write_latex_dynamic_model()
+ParsingDriver::write_latex_dynamic_model(bool write_equation_tags)
 {
-  mod_file->addStatement(new WriteLatexDynamicModelStatement(mod_file->dynamic_model));
+  mod_file->addStatement(new WriteLatexDynamicModelStatement(mod_file->dynamic_model, write_equation_tags));
 }
 
 void
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index bc9729fff4..b25105f437 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -559,7 +559,7 @@ public:
   //! Discretionary policy statement
   void discretionary_policy();
   //! Adds a write_latex_dynamic_model statement
-  void write_latex_dynamic_model();
+  void write_latex_dynamic_model(bool write_equation_tags);
   //! Adds a write_latex_static_model statement
   void write_latex_static_model();
   //! Adds a write_latex_original_model statement
-- 
GitLab