diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index ae94133f0afeec3f5278470541a547cc58cfe558..6026e5146fa9259c64cb8c8aa1b12135405e326c 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -4410,8 +4410,7 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
 
       computeBlockDynJacobianCols();
 
-      if (!no_tmp_terms)
-        computeBlockTemporaryTerms();
+      computeBlockTemporaryTerms(no_tmp_terms);
     }
   else
     {
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index f9842961de1019b48d86a269cf936b18d28bcd72..ee06d07d1e7b8e5b12dc04c3df3c05e94593eca8 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -999,7 +999,7 @@ ModelTree::computeTemporaryTerms(bool is_matlab, bool no_tmp_terms)
 }
 
 void
-ModelTree::computeBlockTemporaryTerms()
+ModelTree::computeBlockTemporaryTerms(bool no_tmp_terms)
 {
   int nb_blocks = blocks.size();
   blocks_temporary_terms.resize(nb_blocks);
@@ -1030,6 +1030,18 @@ ModelTree::computeBlockTemporaryTerms()
       additionalBlockTemporaryTerms(blk, blocks_temporary_terms, reference_count);
     }
 
+  /* If the user has specified the notmpterms option, clear all temporary
+     terms, except those that correspond to external functions (since they are
+     not optional) */
+  if (no_tmp_terms)
+    for (auto &it : blocks_temporary_terms)
+      for (auto &it2 : it)
+        for (auto it3 = it2.begin(); it3 != it2.end();)
+          if (!dynamic_cast<AbstractExternalFunctionNode *>(*it3))
+            it3 = it2.erase(it3);
+          else
+            ++it3;
+
   // Compute indices in the temporary terms vector
   int idx = 0;
   blocks_temporary_terms_idxs.clear();
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 75e96026a12c59fc24fa5caab6e86f58955d2c56..0c54edf96026480faf91ec70463ac5be7277ff01 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -221,7 +221,7 @@ protected:
   //! Computes temporary terms (for all equations and derivatives)
   void computeTemporaryTerms(bool is_matlab, bool no_tmp_terms);
   //! Computes temporary terms per block
-  void computeBlockTemporaryTerms();
+  void computeBlockTemporaryTerms(bool no_tmp_terms);
   /* Add additional temporary terms for a given block. This method is called by
      computeBlockTemporaryTerms(). It does nothing by default, but is meant to
      be overriden by subclasses (actually by DynamicModel, who needs extra
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 7adf0a450f9f907ca6bde1798e08fb06c2bc6337..9efbbcbc3cba03baf0a29c6ab0920ac9e56c6a73 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -1072,8 +1072,7 @@ StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_co
 
       determineLinearBlocks();
 
-      if (!no_tmp_terms)
-        computeBlockTemporaryTerms();
+      computeBlockTemporaryTerms(no_tmp_terms);
     }
   else
     {