From 1cf7f8eac63e1a99ac2d33a3eff59ac4d7c84de7 Mon Sep 17 00:00:00 2001
From: Chris Sangwin <C.J.Sangwin@ed.ac.uk>
Date: Wed, 17 Jul 2024 15:09:08 +0100
Subject: [PATCH] Add example from issue #1064 (texput and hat) to the docs and
 test cases.

---
 doc/en/Authoring/Variables.md |  5 +++++
 tests/castext_test.php        | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/doc/en/Authoring/Variables.md b/doc/en/Authoring/Variables.md
index 6ff43887b..0228b096d 100644
--- a/doc/en/Authoring/Variables.md
+++ b/doc/en/Authoring/Variables.md
@@ -76,6 +76,11 @@ It is also possible to use an unnamed `lambda` function.  E.g. if you have a fun
 
 will display `tup(a,b)` as \( \left[a,b\right) \).
 
+To create a function `hat` so that input `hat(x)` is displayed as \(\hat{x}\) you can use:
+
+    /* In question variables. */
+    texput(hat, lambda([ex], sconcat("\\hat{", tex1(first(ex)), "}")));
+
 As a more complicated example, to typeset `u(A_k,k,1,inf)` as \({\bigcup_{k = 1}^{\infty } {A}_{k}}\) you can use the following:
 
     texput(u,lambda([ex],if length(ex)<4 then return("\\bigcup_{?=?}^{?} ? ") else
diff --git a/tests/castext_test.php b/tests/castext_test.php
index b2861c8a1..89c21cc4c 100644
--- a/tests/castext_test.php
+++ b/tests/castext_test.php
@@ -2025,6 +2025,24 @@ class castext_test extends qtype_stack_testcase {
         $this->assertEquals('\({bigcup_{k = 1}^{\infty } {A}_{k}}\)', $at2->get_rendered());
     }
 
+    /**
+     * @covers \qtype_stack\stack_cas_castext2_latex
+     * @covers \qtype_stack\stack_cas_keyval
+     */
+    public function test_texput_hat() {
+        $vars = 'texput(hat, lambda([ex], sconcat("\\\\hat{", tex1(first(ex)), "}")));';
+        $at1 = new stack_cas_keyval($vars, null, 123);
+        $this->assertTrue($at1->get_valid());
+
+        $cs2 = $at1->get_session();
+        $at2 = castext2_evaluatable::make_from_source('{@1+hat(s)@}', 'test-case');
+        $this->assertTrue($at2->get_valid());
+        $cs2->add_statement($at2);
+        $cs2->instantiate();
+
+        $this->assertEquals('\({\hat{s}+1}\)', $at2->get_rendered());
+    }
+
     /**
      * @covers \qtype_stack\stack_cas_castext2_latex
      * @covers \qtype_stack\stack_cas_keyval
-- 
GitLab