Skip to content

Line endings from maxima LaTeX #1777

Open

Description

@EJMFarrow

There's a bug I've found that's highlighted by freetext but not specific to it. If you import one of the new freetext questions e.g. the multiple extractors question and preview it then the ta in the general feedback ascii block is rendered on a single line. If, however, you go into the edit form and save without any changes, everything works fine and the ta is rendered on multiple lines.

It appears, \n line endings are being stripped from LaTeX from maxima here https://github.com/maths/moodle-qtype_stack/blob/f2b637c297f1b6d8bde0cb5852a95d9f931c0287/locallib.php#L125 but the edit form adds \r\n and the \r lives on?

Not sure there's a backwards compatible fix to that. Any ideas what to do about this, @aharjula ?

After import:
Image

After Save:
Image

ta1:castext("If `f({#x1#})={#f1#}` then `{#x1^2*a#}+b={#f1#}`.

If `f'({#x2#})={#f2#}` then 
`
f'(x)=2 a x
f'({#x2#})=2 a * ({#x2#}) = {#f2#}
{#2*x2#}a={#f2#}
a={#f2/(2*x2)#}
`
Now using this value for `a` we have
`
{#x1^2*a#}+b={#f1#}
{#x1^2*f2/(2*x2)#}+b={#f1#}
b={#tansb#}
`
Hence `f(x)={#taf#}`.");

General feedback:

[[ascii]]{@ta1@}[[/ascii]]

Activity

  1. aharjula commented on Jun 22, 2026

    @aharjula
    Member

    Otherwise, a good theory, but the stack_maxima_latex_tidy is being applied to CASText by the SMLT block, which gets generated to wrap {@...@}-injections, but only if those injections do not inject inline-CASText (this to avoid duplicate application). In your example, the {@ta1@} is a special injection of CASText which gets identified in ct2_latex and should not get the wrapping, well as long as it is not getting any extra processing before that injection and question-variables...

    I would check what the database shows after import and compare that to what it shows after that save. I suspect there is a difference. If not in the database, then maybe in the Moodle cache system. For fun, try transferring questions between courses using Moodle's "course reuse" logic and see if the same happens then. My long-term theory is that this happens during the input of Moodle-XML question import, but as it has typically not been a big enough issue (as long as people don't use //-comments in JS) to truly track down. It might even be at Moodle level...

    Basically, that stack_maxima_latex_tidy should only affect things inside {@...@}, nothing outside those and with the very specific exception of not affecting inline-CASText injections.

  2. EJMFarrow commented on Jun 22, 2026

    @EJMFarrow
    CollaboratorAuthor

    OK, I'm confused then. Updating

        $latex = str_replace("\n ", '', $latex);
        $latex = str_replace("\n", '', $latex);
    

    to

        $latex = str_replace(["\r\n", "\r"], "\n", $latex);
        $latex = str_replace("\n ", "\n", $latex);
    

    displays the imported version correctly.
    Pretty sure the DB versions are different with \n after import and \r\n after save. Will double check.

  3. aharjula commented on Jun 22, 2026

    @aharjula
    Member

    Interesting, I wonder what goes into CAS and comes back if smlt gets applied at that level. Ohh well, in any case, import is not generating \r, but the editor does, which is a rather big difference.

  4. EJMFarrow commented on Jun 22, 2026

    @EJMFarrow
    CollaboratorAuthor

    Have double-checked hex of DB entries and line endings are LF for import and CRLF for save (for question variables at least).

  5. aharjula commented on Jun 22, 2026

    @aharjula
    Member

    Ohh, the fun. I wonder if this varies depending on what sort of OS one runs. Anyway, odd that CRLF saved stuff would export as something else or would be imported as something else.

  6. aharjula commented on Jun 22, 2026

    @aharjula
    Member

    In any case, something is off as smlt should not get applied to ta1, but apparently it is being applied.

    Just to confirm, what does /question/type/stack/adminui/compiledquestion.php?qid=<QID> say? Is the question variables bit for ta1 a list with the form ["%root",...]? (I worry there might be a castext_simplify wrapping around it, and that would merge all the strings...) which should not be done for inline-CASText at that point. And I assume the castext-qt portion has no smlt-blocks visible.

  7. EJMFarrow commented on Jun 22, 2026

    @EJMFarrow
    CollaboratorAuthor

    Here's the output from the imported version of the question:

    Imported.txt

  8. aharjula commented on Jun 22, 2026

    @aharjula
    Member

    Thank you. So the reason why it goes to smlt is because it cannot distinguish between "flat" blocks ({#...#} is a flat block) generating a singular "string" through sconcat and a normal "string" injection. Hmm... I wonder if I need to make inline-CASText generate some other wrapping to make sure this does not happen or ensure that the compile time simplification of inline-CASText does not do that much simplification.

  9. aharjula commented on Jun 26, 2026

    @aharjula
    Member

    Ok, so the "fix" in the pull request should stop SMLT from being applied to the case in this issue, but we still have that \n and \r\n issue in play; it just happened to show this other thing.

    You don't even need to pull that to see if extraneous SMLT was the whole issue; we already had an alternative injection logic. Basically, instead of {@ta1@} put [[castext evaluated="ta1"/]] inside the [[ascii]]-block.

  10. modified the milestones: 4.13.0, 4.14.0 on Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions