Newbie: code examples in problem description

Hi
how can I insert code examples in a problem description? I tried to wrap them in <code>- or <pre> tags, but if the example is in html it will be simply stripped or create an error on save. For the html component there is the visual editor with the code button, but for the problem I can not find a way.
I want to import some hundreds of questions and need a reliable way to import them even if they contain code examples in html.

2 Likes

I’ve tested basic HTML tags and they work flawlessly alongside the problem’s markdown.

You can use <pre>your code here</pre> tags :
I’d rather use <code>your code here</code> since they use a monospace font. code tag itself is rendered inline, if you want it multi-line add a css display class to the tag as follows :

<code style="display:block">
your multi-line
code here
</code>

Basic markdown syntax for code "between grave accents `like this` seems to render as mathematical expressions instead.

I afforded to use HTML tags anywhere in the problem definition:

  • As free text
  • In the >>question prompt<<
  • In the answer choices for choice questions except dropdown.
  • In hints and answer explanations
  • etc.

I don’t see html in your example, so there won’t be a problem.
I tried this:

<problem>
<multiplechoiceresponse>
  <p>Consider the following script:</p>
<pre>
  <code>
  <html>
<head>
  <title>PHP</title>
</head>
<body>
 <?php echo 'This is some sample text'; ?>
</body>
  </html>
  </code>
  </pre>
<label></label>

<choicegroup type="MultipleChoice">
<choice correct="false">an incorrect answer</choice>
<choice correct="true">the correct answer</choice>
<choice correct="false">an incorrect answer</choice>
  </choicegroup>
</multiplechoiceresponse>
</problem>

And everything between the <code> tags will be stripped.
If I create an HTML Component, I have the Visual Editor which converts to:

<p>Consider the following script:</p>
<pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;PHP&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;?php echo&nbsp;'This&nbsp;is&nbsp;some&nbsp;sample&nbsp;text'; ?&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>

So it is displayed. I need the same behaviour in problem editor…

This is because of the limitations of Markdown itself. As the spec says : Markdown supports only a subset of HTML tags.

I’ve found this on stackoverflow:

It seems the only option available is to escape your < and & characters inside code tags by using &lt; and &amp; notation instead You can do that easily through some search & replace