diff --git a/ComputingTasks.cc b/ComputingTasks.cc
index 7cbf05faa0cbc3573dcb3f933596fd88808832b4..e9cc254b9d4c678967172463f7587be7d0fc6894 100644
--- a/ComputingTasks.cc
+++ b/ComputingTasks.cc
@@ -626,9 +626,7 @@ ForecastStatement::writeJsonOutput(ostream &output) const
   output << "}";
 }
 
-RamseyModelStatement::RamseyModelStatement(const SymbolList &symbol_list_arg,
-                                           const OptionsList &options_list_arg) :
-  symbol_list(symbol_list_arg),
+RamseyModelStatement::RamseyModelStatement(const OptionsList &options_list_arg) :
   options_list(options_list_arg)
 {
 }
@@ -2606,15 +2604,16 @@ WriteLatexDynamicModelStatement::writeJsonOutput(ostream &output) const
   output << "{\"statementName\": \"write_latex_dynamic_model\"}";
 }
 
-WriteLatexStaticModelStatement::WriteLatexStaticModelStatement(const StaticModel &static_model_arg) :
-  static_model(static_model_arg)
+WriteLatexStaticModelStatement::WriteLatexStaticModelStatement(const StaticModel &static_model_arg, bool write_equation_tags_arg) :
+  static_model(static_model_arg),
+  write_equation_tags(write_equation_tags_arg)
 {
 }
 
 void
 WriteLatexStaticModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
-  static_model.writeLatexFile(basename);
+  static_model.writeLatexFile(basename, write_equation_tags);
 }
 
 void
@@ -2623,15 +2622,16 @@ WriteLatexStaticModelStatement::writeJsonOutput(ostream &output) const
   output << "{\"statementName\": \"write_latex_static_model\"}";
 }
 
-WriteLatexOriginalModelStatement::WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg) :
-  original_model(original_model_arg)
+WriteLatexOriginalModelStatement::WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg, bool write_equation_tags_arg) :
+  original_model(original_model_arg),
+  write_equation_tags(write_equation_tags_arg)
 {
 }
 
 void
 WriteLatexOriginalModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
-  original_model.writeLatexOriginalFile(basename);
+  original_model.writeLatexOriginalFile(basename, write_equation_tags);
 }
 
 void
@@ -4608,10 +4608,13 @@ void
 CalibSmootherStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
   options_list.writeOutput(output);
+  OptionsList::string_options_t::const_iterator it = options_list.string_options.find("parameter_set");
+  if (it == options_list.string_options.end())
+    output << "options_.parameter_set = 'calibration';" << endl;
   symbol_list.writeOutput("var_list_", output);
-  output << "options_.smoother = 1;" << endl;
-  output << "options_.order = 1;" << endl;
-  output << "[oo_,M_,options_,bayestopt_]=evaluate_smoother('calibration',var_list_,M_,oo_,options_,bayestopt_,estim_params_);" << endl;
+  output << "options_.smoother = 1;" << endl
+         << "options_.order = 1;" << endl
+         << "[oo_, M_, options_, bayestopt_] = evaluate_smoother(options_.parameter_set, var_list_, M_, oo_, options_, bayestopt_, estim_params_);" << endl;
 }
 
 void
diff --git a/ComputingTasks.hh b/ComputingTasks.hh
index 4fdd125643058e5d152fcf58cd497405dee5ca32..81bb5ef2d5b0aa8c983fc6aec0db84e5201e6835 100644
--- a/ComputingTasks.hh
+++ b/ComputingTasks.hh
@@ -186,11 +186,9 @@ public:
 class RamseyModelStatement : public Statement
 {
 private:
-  const SymbolList symbol_list;
   const OptionsList options_list;
 public:
-  RamseyModelStatement(const SymbolList &symbol_list_arg,
-                       const OptionsList &options_list_arg);
+  RamseyModelStatement(const OptionsList &options_list_arg);
   virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
   virtual void writeJsonOutput(ostream &output) const;
@@ -662,8 +660,9 @@ class WriteLatexStaticModelStatement : public Statement
 {
 private:
   const StaticModel &static_model;
+  const bool write_equation_tags;
 public:
-  WriteLatexStaticModelStatement(const StaticModel &static_model_arg);
+  WriteLatexStaticModelStatement(const StaticModel &static_model_arg, bool write_equation_tags_arg);
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
   virtual void writeJsonOutput(ostream &output) const;
 };
@@ -672,8 +671,9 @@ class WriteLatexOriginalModelStatement : public Statement
 {
 private:
   const DynamicModel &original_model;
+  const bool write_equation_tags;
 public:
-  WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg);
+  WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg, bool write_equation_tags_arg);
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
   virtual void writeJsonOutput(ostream &output) const;
 };
diff --git a/DataTree.cc b/DataTree.cc
index 68a96e40ea2f4051eb49332f7ecb5f3e04048cf5..50f668ae6babbea79cea4a8998c27339bc695333 100644
--- a/DataTree.cc
+++ b/DataTree.cc
@@ -537,6 +537,7 @@ DataTree::AddLocalVariable(int symb_id, expr_t value) throw (LocalVariableExcept
     throw LocalVariableException(symbol_table.getName(symb_id));
 
   local_variables_table[symb_id] = value;
+  local_variables_vector.push_back(symb_id);
 }
 
 expr_t
diff --git a/DataTree.hh b/DataTree.hh
index 33049c783c96051befed8c2371ba7d50d08047f8..3cda0bf2a7524583e5d8c3492fc07e838ee73d04 100644
--- a/DataTree.hh
+++ b/DataTree.hh
@@ -90,6 +90,8 @@ protected:
 
   //! Stores local variables value (maps symbol ID to corresponding node)
   map<int, expr_t> local_variables_table;
+  //! Stores the order of appearance of local variables in the model block. Needed following change in #563
+  vector<int> local_variables_vector;
 
   //! Internal implementation of AddVariable(), without the check on the lag
   VariableNode *AddVariableInternal(int symb_id, int lag);
diff --git a/DynamicModel.cc b/DynamicModel.cc
index 79a630c5d0e560a59dc8b784c962a1412488c6a6..aba9a8740b3d00600191aaab4042dba3a186c345 100644
--- a/DynamicModel.cc
+++ b/DynamicModel.cc
@@ -2695,23 +2695,24 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
              (julia ? "false" : "0"))
          << ";" << endl;
 
+  vector<int> state_var;
+  for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
+    // Loop on periods
+    for (int lag = -max_endo_lag; lag < 0; lag++)
+      try
+        {
+          getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag);
+          if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end())
+            state_var.push_back(variable_reordered[endoID]+1);
+        }
+      catch (UnknownDerivIDException &e)
+        {
+        }
+
   //In case of sparse model, writes the block_decomposition structure of the model
   if (block_decomposition)
     {
-      vector<int> state_var, state_equ;
-      for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
-        // Loop on periods
-        for (int lag = -max_endo_lag; lag < 0; lag++)
-          try
-            {
-              getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag);
-              if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end())
-                state_var.push_back(variable_reordered[endoID]+1);
-            }
-          catch (UnknownDerivIDException &e)
-            {
-            }
-
+      vector<int> state_equ;
       int count_lead_lag_incidence = 0;
       int max_lead, max_lag, max_lag_endo, max_lead_endo, max_lag_exo, max_lead_exo, max_lag_exo_det, max_lead_exo_det;
       unsigned int nb_blocks = getNbBlocks();
@@ -3129,13 +3130,13 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
             KF_index_file.write(reinterpret_cast<char *>(&(*it)), sizeof(index_KF));
           KF_index_file.close();
         }
-      output << modstruct << "state_var = [";
-
-      for (vector<int>::const_iterator it = state_var.begin(); it != state_var.end(); it++)
-        output << *it << " ";
-      output << "];" << endl;
     }
 
+  output << modstruct << "state_var = [";
+  for (vector<int>::const_iterator it=state_var.begin(); it != state_var.end(); it++)
+    output << *it << " ";
+  output << "];" << endl;
+
   // Writing initialization for some other variables
   if (!julia)
     output << modstruct << "exo_names_orig_ord = [1:" << symbol_table.exo_nbr() << "];" << endl;
@@ -3818,9 +3819,9 @@ DynamicModel::cloneDynamic(DynamicModel &dynamic_model) const
   assert(&symbol_table == &dynamic_model.symbol_table);
 
   // Convert model local variables (need to be done first)
-  for (map<int, expr_t>::const_iterator it = local_variables_table.begin();
-       it != local_variables_table.end(); it++)
-    dynamic_model.AddLocalVariable(it->first, it->second->cloneDynamic(dynamic_model));
+  for (vector<int>::const_iterator it = local_variables_vector.begin();
+       it != local_variables_vector.end(); it++)
+    dynamic_model.AddLocalVariable(*it, local_variables_table.find(*it)->second->cloneDynamic(dynamic_model));
 
   // Convert equations
   for (size_t i = 0; i < equations.size(); i++)
@@ -3841,7 +3842,8 @@ DynamicModel::cloneDynamic(DynamicModel &dynamic_model) const
   // Convert static_only equations
   for (size_t i = 0; i < static_only_equations.size(); i++)
     dynamic_model.addStaticOnlyEquation(static_only_equations[i]->cloneDynamic(dynamic_model),
-                                        static_only_equations_lineno[i]);
+                                        static_only_equations_lineno[i],
+                                        static_only_equations_equation_tags[i]);
 
   dynamic_model.setLeadsLagsOrig();
 }
@@ -3942,9 +3944,9 @@ DynamicModel::toStatic(StaticModel &static_model) const
   assert(&symbol_table == &static_model.symbol_table);
 
   // Convert model local variables (need to be done first)
-  for (map<int, expr_t>::const_iterator it = local_variables_table.begin();
-       it != local_variables_table.end(); it++)
-    static_model.AddLocalVariable(it->first, it->second->toStatic(static_model));
+  for (vector<int>::const_iterator it = local_variables_vector.begin();
+       it != local_variables_vector.end(); it++)
+    static_model.AddLocalVariable(*it, local_variables_table.find(*it)->second->toStatic(static_model));
 
   // Convert equations
   int static_only_index = 0;
@@ -3952,12 +3954,14 @@ DynamicModel::toStatic(StaticModel &static_model) const
     {
       // Detect if equation is marked [dynamic]
       bool is_dynamic_only = false;
+      vector<pair<string, string> > eq_tags;
       for (vector<pair<int, pair<string, string> > >::const_iterator it = equation_tags.begin();
            it != equation_tags.end(); ++it)
-        if (it->first == i && it->second.first == "dynamic")
+        if (it->first == i)
           {
-            is_dynamic_only = true;
-            break;
+            eq_tags.push_back(it->second);
+            if (it->second.first == "dynamic")
+              is_dynamic_only = true;
           }
 
       try
@@ -3965,11 +3969,11 @@ DynamicModel::toStatic(StaticModel &static_model) const
           // If yes, replace it by an equation marked [static]
           if (is_dynamic_only)
             {
-              static_model.addEquation(static_only_equations[static_only_index]->toStatic(static_model), static_only_equations_lineno[static_only_index]);
+              static_model.addEquation(static_only_equations[static_only_index]->toStatic(static_model), static_only_equations_lineno[static_only_index], static_only_equations_equation_tags[static_only_index]);
               static_only_index++;
             }
           else
-            static_model.addEquation(equations[i]->toStatic(static_model), equations_lineno[i]);
+            static_model.addEquation(equations[i]->toStatic(static_model), equations_lineno[i], eq_tags);
         }
       catch (DataTree::DivisionByZeroException)
         {
@@ -4548,9 +4552,9 @@ DynamicModel::writeLatexFile(const string &basename, const bool write_equation_t
 }
 
 void
-DynamicModel::writeLatexOriginalFile(const string &basename) const
+DynamicModel::writeLatexOriginalFile(const string &basename, const bool write_equation_tags) const
 {
-  writeLatexModelFile(basename + "_original", oLatexDynamicModel);
+  writeLatexModelFile(basename + "_original", oLatexDynamicModel, write_equation_tags);
 }
 
 void
@@ -4870,13 +4874,18 @@ DynamicModel::isModelLocalVariableUsed() const
 }
 
 void
-DynamicModel::addStaticOnlyEquation(expr_t eq, int lineno)
+DynamicModel::addStaticOnlyEquation(expr_t eq, int lineno, const vector<pair<string, string> > &eq_tags)
 {
   BinaryOpNode *beq = dynamic_cast<BinaryOpNode *>(eq);
   assert(beq != NULL && beq->get_op_code() == oEqual);
 
+  vector<pair<string, string> > soe_eq_tags;
+  for (size_t i = 0; i < eq_tags.size(); i++)
+    soe_eq_tags.push_back(eq_tags[i]);
+
   static_only_equations.push_back(beq);
   static_only_equations_lineno.push_back(lineno);
+  static_only_equations_equation_tags.push_back(soe_eq_tags);
 }
 
 size_t
diff --git a/DynamicModel.hh b/DynamicModel.hh
index 712d2ccaee19c31359582aa7f8d58ed2b4b88807..9bc5c29591505caf44c21095a3fe4947dc2b1538 100644
--- a/DynamicModel.hh
+++ b/DynamicModel.hh
@@ -39,6 +39,9 @@ private:
   //! Stores line numbers of equations declared as [static]
   vector<int> static_only_equations_lineno;
 
+  //! Stores the equation tags of equations declared as [static]
+  vector<vector<pair<string, string> > > static_only_equations_equation_tags;
+
   typedef map<pair<int, int>, int> deriv_id_table_t;
   //! Maps a pair (symbol_id, lag) to a deriv ID
   deriv_id_table_t deriv_id_table;
@@ -308,7 +311,7 @@ public:
   void replaceMyEquations(DynamicModel &dynamic_model) const;
 
   //! Adds an equation marked as [static]
-  void addStaticOnlyEquation(expr_t eq, int lineno);
+  void addStaticOnlyEquation(expr_t eq, int lineno, const vector<pair<string, string> > &eq_tags);
 
   //! Returns number of static only equations
   size_t staticOnlyEquationsNbr() const;
@@ -320,7 +323,7 @@ public:
   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;
+  void writeLatexOriginalFile(const string &basename, const bool write_equation_tags) const;
 
   virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
   virtual int getDynJacobianCol(int deriv_id) const throw (UnknownDerivIDException);
diff --git a/DynareBison.yy b/DynareBison.yy
index 48dbca09c932d48f5b81df30b773d6f4900fe000..7b68dcd330efbc8f1afb59e7e25ab65bb9d5f2e3 100644
--- a/DynareBison.yy
+++ b/DynareBison.yy
@@ -129,7 +129,7 @@ class ParsingDriver;
 %token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL
 %token <string_val> TEX_NAME
 %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED
-%token VALUES VAR VAREXO VAREXO_DET VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECTATION PLOT_SHOCK_DECOMPOSITION
+%token VALUES VAR VAREXO VAREXO_DET VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECTATION PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE
 %token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL CROSSEQUATIONS COVARIANCE
 %token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER COLORMAP VAR_MODEL QOQ YOY AOA
 %left COMMA
@@ -203,6 +203,7 @@ statement : parameters
           | varexo
           | varexo_det
           | predetermined_variables
+          | model_local_variable
           | change_type
           | periods
           | model
@@ -494,6 +495,8 @@ predetermined_variables : PREDETERMINED_VARIABLES predetermined_variables_list '
 
 parameters : PARAMETERS parameter_list ';';
 
+model_local_variable : MODEL_LOCAL_VARIABLE model_local_variable_list ';';
+
 named_var_elem : symbol EQUAL QUOTED_STRING
                {
                   pair<string *, string *> *pr = new pair<string *, string *>($1, $3);
@@ -638,6 +641,20 @@ predetermined_variables_list : predetermined_variables_list symbol
                                { driver.add_predetermined_variable($1); }
                              ;
 
+model_local_variable_list : model_local_variable_list symbol
+                            { driver.declare_model_local_variable($2); }
+                          | model_local_variable_list COMMA symbol
+                            { driver.declare_model_local_variable($3); }
+                          | symbol
+                            { driver.declare_model_local_variable($1); }
+                          | model_local_variable_list symbol TEX_NAME
+                            { driver.declare_model_local_variable($2, $3); }
+                          | model_local_variable_list COMMA symbol TEX_NAME
+                            { driver.declare_model_local_variable($3, $4); }
+                          | symbol TEX_NAME
+                            { driver.declare_model_local_variable($1, $2); }
+                          ;
+
 change_type : CHANGE_TYPE '(' change_type_arg ')' change_type_var_list ';'
               { driver.change_type($3, $5); }
             ;
@@ -2169,10 +2186,6 @@ ramsey_model : RAMSEY_MODEL ';'
                 { driver.ramsey_model(); }
               | RAMSEY_MODEL '(' ramsey_model_options_list ')' ';'
                 { driver.ramsey_model(); }
-              | RAMSEY_MODEL symbol_list ';'
-                { driver.ramsey_model(); }
-              | RAMSEY_MODEL '(' ramsey_model_options_list ')' symbol_list ';'
-                { driver.ramsey_model(); }
               ;
 
 ramsey_policy : RAMSEY_POLICY ';'
@@ -2253,11 +2266,15 @@ write_latex_dynamic_model : WRITE_LATEX_DYNAMIC_MODEL ';'
                           ;
 
 write_latex_static_model : WRITE_LATEX_STATIC_MODEL ';'
-                           { driver.write_latex_static_model(); }
+                           { driver.write_latex_static_model(false); }
+                          | WRITE_LATEX_STATIC_MODEL '(' WRITE_EQUATION_TAGS ')' ';'
+                            { driver.write_latex_static_model(true); }
                          ;
 
 write_latex_original_model : WRITE_LATEX_ORIGINAL_MODEL ';'
-                           { driver.write_latex_original_model(); }
+                           { driver.write_latex_original_model(false); }
+                          | WRITE_LATEX_ORIGINAL_MODEL '(' WRITE_EQUATION_TAGS ')' ';'
+                            { driver.write_latex_original_model(true); }
                          ;
 
 shock_decomposition : SHOCK_DECOMPOSITION ';'
@@ -2818,6 +2835,7 @@ calib_smoother_option : o_filtered_vars
                       | o_diffuse_kalman_tol
                       | o_diffuse_filter
                       | o_smoothed_state_uncertainty
+                      | o_parameter_set
                       ;
 
 extended_path : EXTENDED_PATH ';'
diff --git a/DynareFlex.ll b/DynareFlex.ll
index 811b817c48d335c79a5cd6a1de9a45247a52eba7..e1f405e52110921d9c9ab34a6b68b929723db015 100644
--- a/DynareFlex.ll
+++ b/DynareFlex.ll
@@ -111,6 +111,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
 <INITIAL>log_trend_var {BEGIN DYNARE_STATEMENT; return token::LOG_TREND_VAR;}
 <INITIAL>predetermined_variables {BEGIN DYNARE_STATEMENT; return token::PREDETERMINED_VARIABLES;}
 <INITIAL>parameters {BEGIN DYNARE_STATEMENT; return token::PARAMETERS;}
+<INITIAL>model_local_variable {BEGIN DYNARE_STATEMENT; return token::MODEL_LOCAL_VARIABLE;}
 <INITIAL>periods 	{BEGIN DYNARE_STATEMENT; return token::PERIODS;}
 <INITIAL>model_info {BEGIN DYNARE_STATEMENT; return token::MODEL_INFO;}
 <INITIAL>estimation {BEGIN DYNARE_STATEMENT; return token::ESTIMATION;}
diff --git a/DynareMain.cc b/DynareMain.cc
index 49c26025202f3012e5ddb8566fff164bffd62316..bee2ce3a3c9b88a476784ca55d38bc44bd98cb61 100644
--- a/DynareMain.cc
+++ b/DynareMain.cc
@@ -62,7 +62,7 @@ usage()
 #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
        << " [cygwin] [msvc] [mingw]"
 #endif
-       << "[json=parse|check|transform|compute] [jsonstdout] [onlyjson] [jsonderivsimple]"
+       << " [json=parse|check|transform|compute] [jsonstdout] [onlyjson] [jsonderivsimple] [nopathchange] [nopreprocessoroutput]"
        << endl;
   exit(EXIT_FAILURE);
 }
diff --git a/ModFile.cc b/ModFile.cc
index 489229cbafb16e4669e50cc074fd1aa578268b77..9306e32caca4e3d98f59e6cf84b8327497d23e0e 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -829,7 +829,8 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
 
   mOutputFile << "M_.orig_eq_nbr = " << mod_file_struct.orig_eq_nbr << ";" << endl
               << "M_.eq_nbr = " << dynamic_model.equation_number() << ";" << endl
-              << "M_.ramsey_eq_nbr = " << mod_file_struct.ramsey_eq_nbr << ";" << endl;
+              << "M_.ramsey_eq_nbr = " << mod_file_struct.ramsey_eq_nbr << ";" << endl
+              << "M_.set_auxiliary_variables = exist(['./' M_.fname '_set_auxiliary_variables.m'], 'file') == 2;" << endl;
 
   if (dynamic_model.equation_number() > 0)
     {
diff --git a/ModelTree.cc b/ModelTree.cc
index 2282f292d1b442a724637c7123598477d5e6d853..cb3523a627c1c7070b6cccced67a765aa47d1108 100644
--- a/ModelTree.cc
+++ b/ModelTree.cc
@@ -1418,24 +1418,25 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t
   for (size_t i = 0; i < equations.size(); i++)
     equations[i]->collectVariables(eModelLocalVariable, used_local_vars);
 
-  for (set<int>::const_iterator it = used_local_vars.begin();
-       it != used_local_vars.end(); ++it)
-    {
-      int id = *it;
-      expr_t value = local_variables_table.find(id)->second;
-      value->writeExternalFunctionOutput(output, output_type, tt, tef_terms);
-
-      if (IS_C(output_type))
-        output << "double ";
-      else if (IS_JULIA(output_type))
-        output << "  @inbounds ";
-
-      /* We append underscores to avoid name clashes with "g1" or "oo_" (see
-         also VariableNode::writeOutput) */
-      output << symbol_table.getName(id) << "__ = ";
-      value->writeOutput(output, output_type, tt, tef_terms);
-      output << ";" << endl;
-    }
+  for (vector<int>::const_iterator it = local_variables_vector.begin();
+       it != local_variables_vector.end(); it++)
+    if (used_local_vars.find(*it) != used_local_vars.end())
+      {
+        int id = *it;
+        expr_t value = local_variables_table.find(id)->second;
+        value->writeExternalFunctionOutput(output, output_type, tt, tef_terms);
+
+        if (IS_C(output_type))
+          output << "double ";
+        else if (IS_JULIA(output_type))
+          output << "  @inbounds ";
+
+        /* We append underscores to avoid name clashes with "g1" or "oo_" (see
+           also VariableNode::writeOutput) */
+        output << symbol_table.getName(id) << "__ = ";
+        value->writeOutput(output, output_type, tt, tef_terms);
+        output << ";" << endl;
+      }
 }
 
 void
@@ -1468,21 +1469,26 @@ ModelTree::writeJsonModelLocalVariables(ostream &output, deriv_node_temp_terms_t
     }
   output << "]"
          << ", \"model_local_variables\": [";
-  for (set<int>::const_iterator it = used_local_vars.begin();
-       it != used_local_vars.end(); ++it)
-    {
-      if (it != used_local_vars.begin())
-        output << ", ";
-      int id = *it;
-      expr_t value = local_variables_table.find(id)->second;
+  bool printed = false;
+  for (vector<int>::const_iterator it = local_variables_vector.begin();
+       it != local_variables_vector.end(); it++)
+    if (used_local_vars.find(*it) != used_local_vars.end())
+      {
+        int id = *it;
+        expr_t value = local_variables_table.find(id)->second;
 
-      /* We append underscores to avoid name clashes with "g1" or "oo_" (see
-         also VariableNode::writeOutput) */
-      output << "{\"variable\": \"" << symbol_table.getName(id) << "__\""
-             << ", \"value\": \"";
-      value->writeJsonOutput(output, tt, tef_terms);
-      output << "\"}" << endl;
-    }
+        if (printed)
+          output << ", ";
+        else
+          printed = true;
+
+        /* We append underscores to avoid name clashes with "g1" or "oo_" (see
+           also VariableNode::writeOutput) */
+        output << "{\"variable\": \"" << symbol_table.getName(id) << "__\""
+               << ", \"value\": \"";
+        value->writeJsonOutput(output, tt, tef_terms);
+        output << "\"}" << endl;
+      }
   output << "]";
 }
 
@@ -1660,11 +1666,11 @@ ModelTree::writeLatexModelFile(const string &basename, ExprNodeOutputType output
          << "\\footnotesize" << endl;
 
   // Write model local variables
-  for (map<int, expr_t>::const_iterator it = local_variables_table.begin();
-       it != local_variables_table.end(); it++)
+  for (vector<int>::const_iterator it = local_variables_vector.begin();
+       it != local_variables_vector.end(); it++)
     {
-      int id = it->first;
-      expr_t value = it->second;
+      int id = *it;
+      expr_t value = local_variables_table.find(id)->second;
 
       content_output << "\\begin{dmath*}" << endl
                      << symbol_table.getTeXName(id) << " = ";
@@ -1690,7 +1696,7 @@ ModelTree::writeLatexModelFile(const string &basename, ExprNodeOutputType output
 
                 content_output << iteqt->second.first;
 
-                if (iteqt->second.second.empty())
+                if (!(iteqt->second.second.empty()))
                   content_output << "= `" << iteqt->second.second << "'";
 
                 wrote_eq_tag = true;
@@ -1723,7 +1729,7 @@ ModelTree::addEquation(expr_t eq, int lineno)
 }
 
 void
-ModelTree::addEquation(expr_t eq, int lineno, vector<pair<string, string> > &eq_tags)
+ModelTree::addEquation(expr_t eq, int lineno, const vector<pair<string, string> > &eq_tags)
 {
   int n = equations.size();
   for (size_t i = 0; i < eq_tags.size(); i++)
diff --git a/ModelTree.hh b/ModelTree.hh
index 24db008abc4b05dcddfb974a23020f503f166343..be6b66cbbf7646be6c35e1fa1e2414eadf836bd8 100644
--- a/ModelTree.hh
+++ b/ModelTree.hh
@@ -200,7 +200,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 bool write_equation_tags = false) const;
+  void writeLatexModelFile(const string &basename, ExprNodeOutputType output_type, const bool write_equation_tags) const;
 
   //! Sparse matrix of double to store the values of the Jacobian
   /*! First index is equation number, second index is endogenous type specific ID */
@@ -318,7 +318,7 @@ public:
   //! Declare a node as an equation of the model; also give its line number
   void addEquation(expr_t eq, int lineno);
   //! Declare a node as an equation of the model, also giving its tags
-  void addEquation(expr_t eq, int lineno, vector<pair<string, string> > &eq_tags);
+  void addEquation(expr_t eq, int lineno, const vector<pair<string, string> > &eq_tags);
   //! Declare a node as an auxiliary equation of the model, adding it at the end of the list of auxiliary equations
   void addAuxEquation(expr_t eq);
   //! Returns the number of equations in the model
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index d8b317f0a1a47e8684cf10fc767081f467da2b4b..ae2585f3d9cdcf99845e8413871a333892f3c6fa 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -2126,8 +2126,7 @@ ParsingDriver::ramsey_model()
 {
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
     declare_optimal_policy_discount_factor_parameter(data_tree->One);
-  mod_file->addStatement(new RamseyModelStatement(symbol_list, options_list));
-  symbol_list.clear();
+  mod_file->addStatement(new RamseyModelStatement(options_list));
   options_list.clear();
 }
 
@@ -2165,15 +2164,15 @@ ParsingDriver::write_latex_dynamic_model(bool write_equation_tags)
 }
 
 void
-ParsingDriver::write_latex_static_model()
+ParsingDriver::write_latex_static_model(bool write_equation_tags)
 {
-  mod_file->addStatement(new WriteLatexStaticModelStatement(mod_file->static_model));
+  mod_file->addStatement(new WriteLatexStaticModelStatement(mod_file->static_model, write_equation_tags));
 }
 
 void
-ParsingDriver::write_latex_original_model()
+ParsingDriver::write_latex_original_model(bool write_equation_tags)
 {
-  mod_file->addStatement(new WriteLatexOriginalModelStatement(mod_file->original_model));
+  mod_file->addStatement(new WriteLatexOriginalModelStatement(mod_file->original_model, write_equation_tags));
 }
 
 void
@@ -2409,7 +2408,7 @@ ParsingDriver::add_model_equal(expr_t arg1, expr_t arg2)
       if (!id->isInStaticForm())
         error("An equation tagged [static] cannot contain leads, lags, expectations or STEADY_STATE operators");
 
-      dynamic_model->addStaticOnlyEquation(id, location.begin.line);
+      dynamic_model->addStaticOnlyEquation(id, location.begin.line, eq_tags);
     }
   else
     model_tree->addEquation(id, location.begin.line, eq_tags);
@@ -2424,6 +2423,15 @@ ParsingDriver::add_model_equal_with_zero_rhs(expr_t arg)
   return add_model_equal(arg, model_tree->Zero);
 }
 
+void
+ParsingDriver::declare_model_local_variable(string *name, string *tex_name)
+{
+  declare_symbol(name, eModelLocalVariable, tex_name, NULL);
+  delete name;
+  if (tex_name != NULL)
+    delete tex_name;
+}
+
 void
 ParsingDriver::declare_and_init_model_local_variable(string *name, expr_t rhs)
 {
diff --git a/ParsingDriver.hh b/ParsingDriver.hh
index 9e6cebbe3b57dbd6c34960c4759a1445e55141ed..7ca27e1c29d1e92fc80037f3edab194c177cba26 100644
--- a/ParsingDriver.hh
+++ b/ParsingDriver.hh
@@ -325,6 +325,8 @@ public:
   void declare_parameter(string *name, string *tex_name = NULL, vector<pair<string *, string *> *> *partition_value = NULL);
   //! Declares a VAR variable and adds to symbol_list
   void declare_var_endogenous(string *name);
+  //! Declares a model local variable
+  void declare_model_local_variable(string *name, string *tex_name = NULL);
   //! Declares a statement local variable
   void declare_statement_local_variable(string *name);
   //! Completes a subsample statement
@@ -387,7 +389,7 @@ public:
   void end_shocks(bool overwrite);
   //! Writes a mshocks statement
   void end_mshocks(bool overwrite);
-  //! Adds a deterministic chock or a path element inside a conditional_forecast_paths block
+  //! Adds a deterministic shock or a path element inside a conditional_forecast_paths block
   void add_det_shock(string *var, bool conditional_forecast);
   //! Adds a std error chock
   void add_stderr_shock(string *var, expr_t value);
@@ -591,9 +593,9 @@ public:
   //! Adds a write_latex_dynamic_model statement
   void write_latex_dynamic_model(bool write_equation_tags);
   //! Adds a write_latex_static_model statement
-  void write_latex_static_model();
+  void write_latex_static_model(bool write_equation_tags);
   //! Adds a write_latex_original_model statement
-  void write_latex_original_model();
+  void write_latex_original_model(bool write_equation_tags);
   //! BVAR marginal density
   void bvar_density(string *maxnlags);
   //! BVAR forecast
diff --git a/StaticModel.cc b/StaticModel.cc
index a398d1c4f6c03d686c34ec8c65067ab4ffaf3839..97f4f38b5e87ea846b5ba6030985c3e8c2472d16 100644
--- a/StaticModel.cc
+++ b/StaticModel.cc
@@ -2112,9 +2112,9 @@ StaticModel::writeChainRuleDerivative(ostream &output, int eqr, int varr, int la
 }
 
 void
-StaticModel::writeLatexFile(const string &basename) const
+StaticModel::writeLatexFile(const string &basename, bool write_equation_tags) const
 {
-  writeLatexModelFile(basename + "_static", oLatexStaticModel);
+  writeLatexModelFile(basename + "_static", oLatexStaticModel, write_equation_tags);
 }
 
 void
@@ -2130,6 +2130,11 @@ StaticModel::writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type)
 void
 StaticModel::writeSetAuxiliaryVariables(const string &basename, const bool julia) const
 {
+  ostringstream output_func_body;
+  writeAuxVarRecursiveDefinitions(output_func_body, oMatlabStaticModel);
+
+  if (output_func_body.str().empty())
+    return;
 
   string func_name = basename + "_set_auxiliary_variables";
   string filename = julia ? func_name + ".jl" : func_name + ".m";
@@ -2148,10 +2153,8 @@ StaticModel::writeSetAuxiliaryVariables(const string &basename, const bool julia
          << comment << " Status : Computes static model for Dynare" << endl
          << comment << endl
          << comment << " Warning : this file is generated automatically by Dynare" << endl
-         << comment << "           from model file (.mod)" << endl
-         << endl;
-
-  writeAuxVarRecursiveDefinitions(output, oMatlabStaticModel);
+         << comment << "           from model file (.mod)" << endl << endl
+         << output_func_body.str();
 }
 
 void
diff --git a/StaticModel.hh b/StaticModel.hh
index 2dcc81976458208eb694139962f75d12f48a2d43..8c0e9d4430ba0becd781b85a942e65c8153bb57b 100644
--- a/StaticModel.hh
+++ b/StaticModel.hh
@@ -183,7 +183,7 @@ public:
   void writeParamsDerivativesFile(const string &basename, bool julia) const;
 
   //! Writes LaTeX file with the equations of the static model
-  void writeLatexFile(const string &basename) const;
+  void writeLatexFile(const string &basename, const bool write_equation_tags) const;
 
   //! Writes initializations in oo_.steady_state or steady state file for the auxiliary variables
   void writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const;