diff --git a/doc/dynare.texi b/doc/dynare.texi
index 867fbf475cea8b7ad8c7669dbec6f80765747c88..90a005e211d384f68f3386e7489ebe5534f7aa99 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -95,7 +95,7 @@
 @c %**end of header
 
 @copying
-Copyright @copyright{} 1996-2014, Dynare Team.
+Copyright @copyright{} 1996-2015, Dynare Team.
 
 @quotation
 Permission is granted to copy, distribute and/or modify this document
@@ -1875,10 +1875,33 @@ end;
 
 @end deffn
 
-Dynare has the ability to output the list of model equations to a
-@LaTeX{} file, using the @code{write_latex_dynamic_model} command. The
-static model can also be written with the
-@code{write_latex_static_model} command.
+Dynare has the ability to output the original list of model equations
+to a @LaTeX{} file, using the @code{write_latex_original_model}
+command, the list of transformed model equations using the
+@code{write_latex_dynamic_model} command, and the list of static model
+equations using the @code{write_latex_static_model} command.
+
+@anchor{write_latex_original_model}
+
+@deffn Command write_latex_original_model ;
+
+@descriptionhead
+
+This command creates a @LaTeX{} file containing the model, as defined
+in the model block.
+
+If your @file{.mod} file is @file{@var{FILENAME}.mod}, then Dynare
+will create a file called @file{@var{FILENAME}_dynamic.tex},
+containing the list of all the dynamic model equations.
+
+If @LaTeX{} names were given for variables and parameters
+(@pxref{Variable declarations}), then those will be used; otherwise,
+the plain text names will be used.
+
+Time subscripts (@code{t}, @code{t+1}, @code{t-1}, @dots{}) will be
+appended to the variable names, as @LaTeX{} subscripts.
+
+@end deffn
 
 @anchor{write_latex_dynamic_model}
 
diff --git a/matlab/dr_block.m b/matlab/dr_block.m
index ce437b3f69bc90b12b460c8fcc3afab04a90ce8d..e09eec429214b91e21034b82abad80ffaed22e78 100644
--- a/matlab/dr_block.m
+++ b/matlab/dr_block.m
@@ -33,7 +33,7 @@ function [dr,info,M_,options_,oo_] = dr_block(dr,task,M_,options_,oo_,varargin)
 %   none.
 %  
 
-% Copyright (C) 2010-2013 Dynare Team
+% Copyright (C) 2010-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -72,7 +72,7 @@ end;
 if (options_.bytecode)
     [chck, zz, data]= bytecode('dynamic','evaluate', z, zx, M_.params, dr.ys, 1, data);
 else
-    [r, data] = feval([M_.fname '_dynamic'], z', zx, M_.params, dr.ys, M_.maximum_lag+1, data);
+    [r, data] = feval([M_.fname '_dynamic'], options_, M_, oo_, z', zx, M_.params, dr.ys, M_.maximum_lag+1, data);
     chck = 0;
 end;
 mexErrCheck('bytecode', chck);
diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 5df0edbf80d1e62ea262ddaf1559952f4b2593f4..b6ad8c9601bf40559fb9bc8c55dd1b27b6e5c814 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -16,7 +16,7 @@ function info = dyn_forecast(var_list,task)
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright (C) 2003-2013 Dynare Team
+% Copyright (C) 2003-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -37,6 +37,8 @@ global options_ oo_ M_
 
 info = 0;
 
+make_ex_;
+
 maximum_lag = M_.maximum_lag;
 
 endo_names = M_.endo_names;
@@ -122,15 +124,15 @@ if ~isscalar(trend)
 end
 
 for i=1:n_var
-    eval(['oo_.forecast.Mean.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+1:end)'';']);
-    eval(['oo_.forecast.HPDinf.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+1:end)''-' ...
-          ' int_width(:,' int2str(i) ');']);
-    eval(['oo_.forecast.HPDsup.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+1:end)''+' ...
-          ' int_width(:,' int2str(i) ');']);
+    eval(['oo_.forecast.Mean.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+(1:horizon))'';']);
+    eval(['oo_.forecast.HPDinf.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+(1:horizon))''-' ...
+          ' int_width(1:horizon,' int2str(i) ');']);
+    eval(['oo_.forecast.HPDsup.' var_list(i,:) '= yf(' int2str(i) ',maximum_lag+(1:horizon))''+' ...
+          ' int_width(1:horizon,' int2str(i) ');']);
 end
 
 for i=1:M_.exo_det_nbr
-    eval(['oo_.forecast.Exogenous.' M_.exo_det_names(i,:) '= oo_.exo_det_simul(:,' int2str(i) ');']);
+    eval(['oo_.forecast.Exogenous.' M_.exo_det_names(i,:) '= oo_.exo_det_simul(maximum_lag+(1:horizon),' int2str(i) ');']);
 end
 
 if options_.nograph == 0
diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m
index 7661ff62814ca7e401614fc80d1b6b35be4cae9d..a92cba11db76d1f7d64e0505482ac47432952670 100644
--- a/matlab/dyn_ramsey_static.m
+++ b/matlab/dyn_ramsey_static.m
@@ -14,7 +14,7 @@ function [steady_state,params,check] = dyn_ramsey_static(ys_init,M,options_,oo)
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright (C) 2003-2014 Dynare Team
+% Copyright (C) 2003-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m
index b439039cdc4b181dc10c0f7ca3afcac9b8bd3595..b2544d9b187b8d73eff7b943306466be1f547996 100644
--- a/matlab/dynare_config.m
+++ b/matlab/dynare_config.m
@@ -15,7 +15,7 @@ function dynareroot = dynare_config(path_to_dynare,verbose)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright (C) 2001-2014 Dynare Team
+% Copyright (C) 2001-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
diff --git a/matlab/ep/extended_path.m b/matlab/ep/extended_path.m
index 5cc5e8e1764b20bf4d25b37262950f4182981b8d..bc65010541c6ec878d7f4dd317c5b1d5f98a08fa 100644
--- a/matlab/ep/extended_path.m
+++ b/matlab/ep/extended_path.m
@@ -14,7 +14,7 @@ function [ts,results] = extended_path(initial_conditions,sample_size)
 %
 % SPECIAL REQUIREMENTS
 
-% Copyright (C) 2009-2013 Dynare Team
+% Copyright (C) 2009-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -325,4 +325,4 @@ if info_convergence
     y = endo_simul(:,2);
 else
     y = NaN(size(endo_nbr,1));
-end
\ No newline at end of file
+end
diff --git a/matlab/ep/solve_stochastic_perfect_foresight_model.m b/matlab/ep/solve_stochastic_perfect_foresight_model.m
index 36bed1a30a15de612008e0f2ac57606ce6352b8a..ab13e3043ac6c89cd30f80aff549f6fb7f3b57b6 100644
--- a/matlab/ep/solve_stochastic_perfect_foresight_model.m
+++ b/matlab/ep/solve_stochastic_perfect_foresight_model.m
@@ -1,6 +1,6 @@
 function [flag,endo_simul,err] = solve_stochastic_perfect_foresight_model(endo_simul,exo_simul,pfm,nnodes,order)
 
-% Copyright (C) 2012-2013 Dynare Team
+% Copyright (C) 2012-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -232,4 +232,4 @@ function [flag,endo_simul,err] = solve_stochastic_perfect_foresight_model(endo_s
     end
     if verbose
         disp (['-----------------------------------------------------']) ;
-    end
\ No newline at end of file
+    end
diff --git a/matlab/initial_estimation_checks.m b/matlab/initial_estimation_checks.m
index 7c849540f6e073b7eaa630e315b8cd50091253ad..b78b50b7e1c3216f48205c4adf64b37067ebdafb 100644
--- a/matlab/initial_estimation_checks.m
+++ b/matlab/initial_estimation_checks.m
@@ -19,7 +19,7 @@ function DynareResults = initial_estimation_checks(objective_function,xparam1,Dy
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright (C) 2003-2014 Dynare Team
+% Copyright (C) 2003-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
diff --git a/matlab/lnsrch1_wrapper_two_boundaries.m b/matlab/lnsrch1_wrapper_two_boundaries.m
index 5f4fd356d46f37ea82b40dc9b868fa51b0a669b4..3b55ca70351a1f992f714936ca33078577fc6ee5 100644
--- a/matlab/lnsrch1_wrapper_two_boundaries.m
+++ b/matlab/lnsrch1_wrapper_two_boundaries.m
@@ -1,5 +1,5 @@
 function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ...
-                                             params, steady_state, periods, y_kmin, y_size)
+                                             params, steady_state, periods, y_kmin, y_size,Periods)
 % wrapper for solve_one_boundary m-file when it is used with a dynamic
 % model
 %
@@ -26,7 +26,7 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ...
 %   none.
 %  
 
-% Copyright (C) 2009-2011 Dynare Team
+% Copyright (C) 2009-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -45,5 +45,5 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ...
 
 %reshape the input arguments of the dynamic function
 y(y_kmin+1:y_kmin+periods, y_index) = reshape(ya',length(y_index),periods)';
-[r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, y_size);
+[r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, y_size, Periods);
 ra = reshape(r(:, y_kmin+1:periods+y_kmin),periods*y_size, 1);
diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m
index 9e317b0ed69bbeb0fb3171a243fe24a3c9d745e5..f6f9ee894b501fa1f365f61b591dd946701b9f16 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_solver.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m
@@ -12,7 +12,7 @@ function perfect_foresight_solver()
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright (C) 1996-2014 Dynare Team
+% Copyright (C) 1996-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -164,4 +164,4 @@ else
 end
 
 ts = dseries(transpose(oo_.endo_simul),initial_period,cellstr(M_.endo_names));
-assignin('base', 'Simulated_time_series', ts);
\ No newline at end of file
+assignin('base', 'Simulated_time_series', ts);
diff --git a/matlab/perfect-foresight-models/private/check_input_arguments.m b/matlab/perfect-foresight-models/private/check_input_arguments.m
index a1bef008aff4780703a9c04e70fbd431d595b42d..26d84153d5743e3ffbeedec1f0bc7fa6feb58759 100644
--- a/matlab/perfect-foresight-models/private/check_input_arguments.m
+++ b/matlab/perfect-foresight-models/private/check_input_arguments.m
@@ -1,6 +1,7 @@
 function check_input_arguments(DynareOptions, DynareModel, DynareResults)
+%function check_input_arguments(DynareOptions, DynareModel, DynareResults)
 
-% Copyright (C) 2014 Dynare Team
+% Copyright (C) 2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -43,6 +44,7 @@ if isempty(DynareResults.endo_simul) || any(size(DynareResults.endo_simul) ~= [
     error('perfect_foresight_solver:ArgCheck','PERFECT_FORESIGHT_SOLVER: ''oo_.endo_simul'' has wrong size. Did you run ''perfect_foresight_setup'' ?')
 end
 
-if isempty(DynareResults.exo_simul) || any(size(DynareResults.exo_simul) ~= [ DynareModel.maximum_lag+DynareOptions.periods+DynareModel.maximum_lead, DynareModel.exo_nbr ])
+if (DynareModel.exo_nbr > 0) && (isempty(DynareResults.exo_simul) || ...
+                                 any(size(DynareResults.exo_simul) ~= [ DynareModel.maximum_lag+DynareOptions.periods+DynareModel.maximum_lead, DynareModel.exo_nbr ]))
     error('perfect_foresight_solver:ArgCheck','PERFECT_FORESIGHT_SOLVER: ''oo_.exo_simul'' has wrong size. Did you run ''perfect_foresight_setup'' ?')
 end
diff --git a/matlab/perfect-foresight-models/private/simulation_core.m b/matlab/perfect-foresight-models/private/simulation_core.m
index b7e2617ebfe658c7197a2d4b284f1885943ce721..c266b715eb49e446bcc851e130f285d6a31588f9 100644
--- a/matlab/perfect-foresight-models/private/simulation_core.m
+++ b/matlab/perfect-foresight-models/private/simulation_core.m
@@ -1,4 +1,22 @@
 function [oo_, maxerror] = simulation_core(options_, M_, oo_)
+%function [oo_, maxerror] = simulation_core(options_, M_, oo_)
+
+% Copyright (C) 2015 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
 if options_.block
     if options_.bytecode
@@ -15,7 +33,7 @@ if options_.block
         end
         mexErrCheck('bytecode', info);
     else
-        eval([M_.fname '_dynamic']);
+        oo_ = feval([M_.fname '_dynamic'], options_, M_, oo_);
     end
 else
     if options_.bytecode
diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m
index 3a632227caf02fe42b4ca01ce5c4bfe14b6f82f0..88392f144d0537dd524de82b88d8fa88d3f2a7bc 100644
--- a/matlab/perfect-foresight-models/sim1.m
+++ b/matlab/perfect-foresight-models/sim1.m
@@ -13,7 +13,7 @@ function oo = sim1(M, options, oo)
 % SPECIAL REQUIREMENTS
 %   None.
 
-% Copyright (C) 1996-2013 Dynare Team
+% Copyright (C) 1996-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -216,4 +216,4 @@ end
 if verbose
     disp (['-----------------------------------------------------']) ;
     skipline();
-end
\ No newline at end of file
+end
diff --git a/matlab/perfect-foresight-models/sim1_lbj.m b/matlab/perfect-foresight-models/sim1_lbj.m
index cc7a6b2aa36caa9c00aef77ff45a08659d328b76..141dbf7a9318cae018ef3eb54890f612d3647c94 100644
--- a/matlab/perfect-foresight-models/sim1_lbj.m
+++ b/matlab/perfect-foresight-models/sim1_lbj.m
@@ -16,7 +16,7 @@ function oo_ = sim1_lbj(options_, M_, oo_)
 % SPECIAL REQUIREMENTS
 %   None.
 
-% Copyright (C) 1996-2012 Dynare Team
+% Copyright (C) 1996-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
diff --git a/matlab/perfect-foresight-models/sim1_purely_backward.m b/matlab/perfect-foresight-models/sim1_purely_backward.m
index 65746675ed412cbb35832f3e6f6ce7972d5849f8..21866740f319f15f8596752692b6084ed2227e5d 100644
--- a/matlab/perfect-foresight-models/sim1_purely_backward.m
+++ b/matlab/perfect-foresight-models/sim1_purely_backward.m
@@ -1,7 +1,7 @@
 function oo_ = sim1_purely_backward(options_, M_, oo_)
 % Performs deterministic simulation of a purely backward model
 
-% Copyright (C) 2012-2014 Dynare Team
+% Copyright (C) 2012-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -52,4 +52,4 @@ function oo_ = sim1_purely_backward(options_, M_, oo_)
         end
 
         oo_.endo_simul(:,it) = tmp(nyb+1:nyb+M_.endo_nbr);
-    end
\ No newline at end of file
+    end
diff --git a/matlab/perfect-foresight-models/sim1_purely_forward.m b/matlab/perfect-foresight-models/sim1_purely_forward.m
index b28993fcefe544c88a8be1e9a7593a4bb61efb54..8a8fa3294018fd6ce952254eb617dcd47718b781 100644
--- a/matlab/perfect-foresight-models/sim1_purely_forward.m
+++ b/matlab/perfect-foresight-models/sim1_purely_forward.m
@@ -1,7 +1,7 @@
 function oo_ = sim1_purely_forward(options_, M_, oo_)
 % Performs deterministic simulation of a purely forward model
 
-% Copyright (C) 2012-2014 Dynare Team
+% Copyright (C) 2012-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -44,4 +44,4 @@ function oo_ = sim1_purely_forward(options_, M_, oo_)
         end
 
         oo_.endo_simul(:,it) = tmp(1:M_.endo_nbr);
-    end
\ No newline at end of file
+    end
diff --git a/matlab/solve_two_boundaries.m b/matlab/solve_two_boundaries.m
index e8da69742d5b1de2b932f083a16224304e73d570..cddc8f2da14f76021559165b1143c65e3b00925a 100644
--- a/matlab/solve_two_boundaries.m
+++ b/matlab/solve_two_boundaries.m
@@ -1,4 +1,4 @@
-function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, M, oo)
+function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo,options,M, oo)
 % Computes the deterministic simulation of a block of equation containing
 % both lead and lag variables using relaxation methods 
 %
@@ -46,7 +46,7 @@ function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_inde
 %   none.
 %  
 
-% Copyright (C) 1996-2013 Dynare Team
+% Copyright (C) 1996-2015 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -81,7 +81,7 @@ Jacobian_Size=Blck_size*(y_kmin+y_kmax_l +periods);
 g1=spalloc( Blck_size*periods, Jacobian_Size, nze*periods);
 reduced = 0;
 while ~(cvg==1 || iter>maxit_),
-    [r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, Blck_size);
+    [r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, Blck_size,options.periods);
     preconditioner = 2;
     g1a=g1(:, y_kmin*Blck_size+1:(periods+y_kmin)*Blck_size);
     term1 = g1(:, 1:y_kmin_l*Blck_size)*reshape(y(1+y_kmin-y_kmin_l:y_kmin,y_index)',1,y_kmin_l*Blck_size)';
@@ -298,8 +298,7 @@ while ~(cvg==1 || iter>maxit_),
             g = (ra'*g1a)';
             f = 0.5*ra'*ra;
             p = -g1a\ra;
-            [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax, ...
-                                    'lnsrch1_wrapper_two_boundaries',nn,nn,  fname, y, y_index, x, params, steady_state, periods, y_kmin, Blck_size);
+            [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn,  fname, y, y_index,x, params, steady_state, periods, y_kmin, Blck_size,options.periods);
             dx = ya - yn;
             y(1+y_kmin:periods+y_kmin,y_index)=reshape(yn',length(y_index),periods)';
         end
diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc
index 76cea6dc390e4c8fd570ed3a968641033c028d98..2c0c5d2978abdd8cbc5cac9d499ec41c409b5466 100644
--- a/preprocessor/ComputingTasks.cc
+++ b/preprocessor/ComputingTasks.cc
@@ -1431,6 +1431,17 @@ WriteLatexStaticModelStatement::writeOutput(ostream &output, const string &basen
   static_model.writeLatexFile(basename);
 }
 
+WriteLatexOriginalModelStatement::WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg) :
+  original_model(original_model_arg)
+{
+}
+
+void
+WriteLatexOriginalModelStatement::writeOutput(ostream &output, const string &basename) const
+{
+  original_model.writeLatexOriginalFile(basename);
+}
+
 ShockDecompositionStatement::ShockDecompositionStatement(const SymbolList &symbol_list_arg,
                                                          const OptionsList &options_list_arg) :
   symbol_list(symbol_list_arg),
diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh
index b1a009ffac7e2ed92df869624d27a4762adadd44..36732cc2496eeb7897f72f18c436f1861ef16389 100644
--- a/preprocessor/ComputingTasks.hh
+++ b/preprocessor/ComputingTasks.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -509,6 +509,15 @@ public:
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
+class WriteLatexOriginalModelStatement : public Statement
+{
+private:
+  const DynamicModel &original_model;
+public:
+  WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg);
+  virtual void writeOutput(ostream &output, const string &basename) const;
+};
+
 class ShockDecompositionStatement : public Statement
 {
 private:
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index fab1f84a9fbd0a97b9aa4596edbf5696b401088f..f4efef49b893894bb0db0dd287a4d5029a293a62 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -342,7 +342,7 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
       else if (simulation_type == SOLVE_BACKWARD_SIMPLE || simulation_type == SOLVE_FORWARD_SIMPLE)
         output << "function [residual, y, g1, g2, g3, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, it_, jacobian_eval)\n";
       else
-        output << "function [residual, y, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, periods, jacobian_eval, y_kmin, y_size)\n";
+        output << "function [residual, y, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << block+1 << "(y, x, params, steady_state, periods, jacobian_eval, y_kmin, y_size, Periods)\n";
       BlockType block_type;
       if (simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE || simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE)
         block_type = SIMULTAN;
@@ -364,7 +364,6 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
              << "  % //                     Simulation type "
              << BlockSim(simulation_type) << "  //" << endl
              << "  % ////////////////////////////////////////////////////////////////////////" << endl;
-      output << "  global options_ oo_;" << endl;
       //The Temporary terms
       if (simulation_type == EVALUATE_BACKWARD || simulation_type == EVALUATE_FORWARD)
         {
@@ -385,9 +384,9 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
           output << "  else\n";
           if (simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE || simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE)
             {
-              output << "    g1 = spalloc(" << block_mfs << "*options_.periods, "
-                     << block_mfs << "*(options_.periods+" << max_leadlag_block[block].first+max_leadlag_block[block].second+1 << ")"
-                     << ", " << nze << "*options_.periods);\n";
+              output << "    g1 = spalloc(" << block_mfs << "*Periods, "
+                     << block_mfs << "*(Periods+" << max_leadlag_block[block].first+max_leadlag_block[block].second+1 << ")"
+                     << ", " << nze << "*Periods);\n";
             }
           else
             {
@@ -1789,8 +1788,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
   int Nb_SGE = 0;
   bool open_par = false;
 
-  mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(varargin)\n";
-  mDynamicModelFile << "  global oo_ options_ M_ ;\n";
+  mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(options_, M_, oo_, varargin)\n";
   mDynamicModelFile << "  g2=[];g3=[];\n";
   //Temporary variables declaration
   OK = true;
@@ -1805,7 +1803,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
       (*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms);
     }
   if (tmp_output.str().length() > 0)
-    mDynamicModelFile << "  global " << tmp_output.str() << " M_ ;\n";
+    mDynamicModelFile << "  global " << tmp_output.str() << ";\n";
 
   mDynamicModelFile << "  T_init=zeros(1,options_.periods+M_.maximum_lag+M_.maximum_lead);\n";
   tmp_output.str("");
@@ -1882,7 +1880,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
           break;
         case SOLVE_TWO_BOUNDARIES_COMPLETE:
         case SOLVE_TWO_BOUNDARIES_SIMPLE:
-          mDynamicModelFile << "    [r, y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_xd, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" <<  block + 1 << "(y, x, params, steady_state, it_-" << max_lag << ", 1, " << max_lag << ", " << block_recursive << ");\n";
+          mDynamicModelFile << "    [r, y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_xd, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" <<  block + 1 << "(y, x, params, steady_state, it_-" << max_lag << ", 1, " << max_lag << ", " << block_recursive << "," << "options_.periods" << ");\n";
           mDynamicModelFile << "    residual(y_index_eq)=r(:,M_.maximum_lag+1);\n";
           break;
         default:
@@ -2065,7 +2063,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
                             <<", options_.periods, " << max_leadlag_block[block].first
                             <<", " << max_leadlag_block[block].second
                             <<", " << blocks_linear[block]
-                            <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, M_, oo_);\n";
+                            <<", blck_num, y_kmin, options_.simul.maxit, options_.solve_tolf, options_.slowc, " << cutoff << ", options_.stack_solve_algo, options_, M_, oo_);\n";
           mDynamicModelFile << "  tmp = y(:,M_.block_structure.block(" << block + 1 << ").variable);\n";
           mDynamicModelFile << "  if any(isnan(tmp) | isinf(tmp))\n";
           mDynamicModelFile << "    disp(['Inf or Nan value during the resolution of block " << block <<"']);\n";
@@ -2077,6 +2075,7 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
     mDynamicModelFile << "  end;\n";
   open_par = false;
   mDynamicModelFile << "  oo_.endo_simul = y';\n";
+  mDynamicModelFile << "  varargout{1} = oo_;\n";
   mDynamicModelFile << "return;\n";
   mDynamicModelFile << "end" << endl;
 
@@ -3997,6 +3996,12 @@ DynamicModel::writeLatexFile(const string &basename) const
   writeLatexModelFile(basename + "_dynamic.tex", oLatexDynamicModel);
 }
 
+void
+DynamicModel::writeLatexOriginalFile(const string &basename) const
+{
+  writeLatexModelFile(basename + "_original.tex", oLatexDynamicModel);
+}
+
 void
 DynamicModel::substituteEndoLeadGreaterThanTwo(bool deterministic_model)
 {
diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh
index 7f019f260fc3ffc290bdb3e4159706ba0fe4156f..8efbc5d3c67457efe4f039e6118558016e8e90d7 100644
--- a/preprocessor/DynamicModel.hh
+++ b/preprocessor/DynamicModel.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -252,6 +252,9 @@ public:
   //! Writes LaTeX file with the equations of the dynamic model
   void writeLatexFile(const string &basename) const;
 
+  //! Writes LaTeX file with the equations of the dynamic model (for the original model)
+  void writeLatexOriginalFile(const string &basename) const;
+
   virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
   virtual int getDynJacobianCol(int deriv_id) const throw (UnknownDerivIDException);
   virtual void addAllParamDerivId(set<int> &deriv_id_set);
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 737696d4cbd4975722d4ed7f9400d307fa6fb6fe..3760a46edef7676578078b38542f0a66ad7985a8 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -126,7 +126,7 @@ class ParsingDriver;
 %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 PREDETERMINED_VARIABLES
-%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL
+%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL
 %token XLS_SHEET XLS_RANGE LONG_NAME
 %left COMMA
 %left EQUAL_EQUAL EXCLAMATION_EQUAL
@@ -250,6 +250,7 @@ statement : parameters
           | identification
           | write_latex_dynamic_model
           | write_latex_static_model
+          | write_latex_original_model
           | shock_decomposition
           | conditional_forecast
           | conditional_forecast_paths
@@ -1906,6 +1907,10 @@ write_latex_static_model : WRITE_LATEX_STATIC_MODEL ';'
                            { driver.write_latex_static_model(); }
                          ;
 
+write_latex_original_model : WRITE_LATEX_ORIGINAL_MODEL ';'
+                           { driver.write_latex_original_model(); }
+                         ;
+
 shock_decomposition : SHOCK_DECOMPOSITION ';'
                       {driver.shock_decomposition(); }
                     | SHOCK_DECOMPOSITION '(' shock_decomposition_options_list ')' ';'
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 040ecf82236bf4925c23f7fdf8f31d87c08a65ef..cf65ff855cd7975f5551b7adacfd8239a03313e0 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -129,6 +129,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>save_params_and_steady_state  {BEGIN DYNARE_STATEMENT; return token::SAVE_PARAMS_AND_STEADY_STATE;}
 <INITIAL>write_latex_dynamic_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_DYNAMIC_MODEL;}
 <INITIAL>write_latex_static_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STATIC_MODEL;}
+<INITIAL>write_latex_original_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_ORIGINAL_MODEL;}
 
 <INITIAL>steady {BEGIN DYNARE_STATEMENT; return token::STEADY;}
 <INITIAL>check {BEGIN DYNARE_STATEMENT; return token::CHECK;}
diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc
index d4121b72924600e3e2663709300afab1edd0f2b0..d61ebb56f446f3bea5607e7c9afbe080ec1238f4 100644
--- a/preprocessor/ExprNode.cc
+++ b/preprocessor/ExprNode.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2014 Dynare Team
+ * Copyright (C) 2007-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1851,8 +1851,20 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
       }
       return;
     case oExpectation:
-      cerr << "UnaryOpNode::writeOutput: not implemented on oExpectation" << endl;
-      exit(EXIT_FAILURE);
+      if (!IS_LATEX(output_type))
+        {
+          cerr << "UnaryOpNode::writeOutput: not implemented on oExpectation" << endl;
+          exit(EXIT_FAILURE);
+        }
+      output << "\\mathbb{E}_{t";
+      if (expectation_information_set != 0)
+        {
+          if (expectation_information_set > 0)
+            output << "+";
+          output << expectation_information_set;
+        }
+      output << "}";
+      break;
     case oErf:
       output << "erf";
       break;
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 809255973de8f69766798ce2104112b563f1209f..e21cf81e82819134705edcb0647da96abe33c51f 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2014 Dynare Team
+ * Copyright (C) 2006-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -32,6 +32,7 @@
 
 ModFile::ModFile(WarningConsolidation &warnings_arg)
   : expressions_tree(symbol_table, num_constants, external_functions_table),
+    original_model(symbol_table, num_constants, external_functions_table),
     dynamic_model(symbol_table, num_constants, external_functions_table),
     trend_dynamic_model(symbol_table, num_constants, external_functions_table),
     ramsey_FOC_equations_dynamic_model(symbol_table, num_constants, external_functions_table),
@@ -306,6 +307,9 @@ ModFile::checkPass()
 void
 ModFile::transformPass(bool nostrict)
 {
+  // Save the original model (must be done before any model transformations by preprocessor)
+  dynamic_model.cloneDynamic(original_model);
+
   if (nostrict)
     {
       set<int> unusedEndogs = dynamic_model.findUnusedEndogenous();
diff --git a/preprocessor/ModFile.hh b/preprocessor/ModFile.hh
index 04aa1b5e4ac1451a2a075ee2492c3339e94116c8..722ea534196228fa026b57d52ba607d76bb3786c 100644
--- a/preprocessor/ModFile.hh
+++ b/preprocessor/ModFile.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2014 Dynare Team
+ * Copyright (C) 2006-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -51,6 +51,8 @@ public:
   NumericalConstants num_constants;
   //! Expressions outside model block
   DataTree expressions_tree;
+  //! Original model, as declared in the "model" block, that won't be modified by the preprocessor
+  DynamicModel original_model;
   //! Dynamic model, as declared in the "model" block
   DynamicModel dynamic_model;
   //! A copy of Dynamic model, for testing trends declared by user
diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index 7bdd78aed948bb82c7030dbd85c9fc4eea340d77..688d4f292d888d43c533a40456f871dc2f976cc4 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1352,6 +1352,7 @@ ModelTree::writeLatexModelFile(const string &filename, ExprNodeOutputType output
   output << "\\documentclass[10pt,a4paper]{article}" << endl
          << "\\usepackage[landscape]{geometry}" << endl
          << "\\usepackage{fullpage}" << endl
+         << "\\usepackage{amsfonts}" << endl
          << "\\usepackage{breqn}" << endl
          << "\\begin{document}" << endl
          << "\\footnotesize" << endl;
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index c7f6b2574e07da1a109a9a2da8d79f753f66f2ad..acf0c8a91424c3a871d4258e97321c82da6f1768 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1820,6 +1820,12 @@ ParsingDriver::write_latex_static_model()
   mod_file->addStatement(new WriteLatexStaticModelStatement(mod_file->static_model));
 }
 
+void
+ParsingDriver::write_latex_original_model()
+{
+  mod_file->addStatement(new WriteLatexOriginalModelStatement(mod_file->original_model));
+}
+
 void
 ParsingDriver::bvar_density(string *maxnlags)
 {
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index a747e896cadb97b75ee5a985265c461d6f6bb826..ace9d1223db80c1ab810a7c66fb4a8dac21a72ce 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -503,6 +503,8 @@ public:
   void write_latex_dynamic_model();
   //! Adds a write_latex_static_model statement
   void write_latex_static_model();
+  //! Adds a write_latex_original_model statement
+  void write_latex_original_model();
   //! BVAR marginal density
   void bvar_density(string *maxnlags);
   //! BVAR forecast
diff --git a/preprocessor/macro/MacroFlex.ll b/preprocessor/macro/MacroFlex.ll
index 05a60329c71d35dfa8c62794da03fa5cfad0a7e6..be908019ce23f3f3c902fa9ab2ee051e53eae7a0 100644
--- a/preprocessor/macro/MacroFlex.ll
+++ b/preprocessor/macro/MacroFlex.ll
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2013 Dynare Team
+ * Copyright (C) 2008-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -190,7 +190,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <FOR_BODY>.                 { for_body_tmp.append(yytext); yylloc->step(); }
-<FOR_BODY><<EOF>>           { driver.error(for_stmt_loc_tmp, "@#for loop not matched by an @#endfor (unexpected end of file)"); }
+<FOR_BODY><<EOF>>           { driver.error(for_stmt_loc_tmp, "@#for loop not matched by an @#endfor or file does not end with a new line (unexpected end of file)"); }
 <FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*(\/\/.*)?{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -227,7 +227,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <THEN_BODY>.                { then_body_tmp.append(yytext); yylloc->step(); }
-<THEN_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if/@#ifdef/@#ifndef not matched by an @#endif (unexpected end of file)"); }
+<THEN_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if/@#ifdef/@#ifndef not matched by an @#endif or file does not end with a new line (unexpected end of file)"); }
 <THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*(\/\/.*)?{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -269,7 +269,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <ELSE_BODY>.                { else_body_tmp.append(yytext); yylloc->step(); }
-<ELSE_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if/@#ifdef/@#ifndef not matched by an @#endif (unexpected end of file)"); }
+<ELSE_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if/@#ifdef/@#ifndef not matched by an @#endif or file does not end with a new line (unexpected end of file)"); }
 
 <ELSE_BODY>^{SPC}*@#{SPC}*endif{SPC}*(\/\/.*)?{EOL} {
                               yylloc->lines(1);
diff --git a/tests/expectations/expectation.mod b/tests/expectations/expectation.mod
index c05d7528d1771ed9ae3fb537d195c1db5a641a91..23a36a0e673f47e25603466a260e122fc95f25fa 100644
--- a/tests/expectations/expectation.mod
+++ b/tests/expectations/expectation.mod
@@ -35,6 +35,8 @@ e = 0;
 u = 0;
 end;
 
+write_latex_original_model;
+
 shocks;
 var e; stderr 0.009;
 var u; stderr 0.009;