Avoid repetitive style specifications in markdown

I’m writing a post in markdown and I’m stuck at the following screenshot

The stuff in the red rectangle is cumbersome to reproduce if using inline style like:

<div style="text-align: center">
  <code style="background: none">\expandafter</code> 
  <span style="font-size: 130%">&langle;</span>token<sub>e</sub><span style="font-size: 130%">&rangle;</span>
  <span style="font-size: 130%">&langle;</span>token<sub>1</sub><span style="font-size: 130%">&rangle;</span>
</div>

Though the effect is satisfactory, I have lots of token blocks to input. So I’m wondering whether there is some method that can help me reduce the work of repetitive style specifications.

Any hints are appreciated!

Try using mathJax. You can use jekyll-spaceship plugin to get mathJax support easily.

I’m using katex right now. So I don’t really want to switch to MathJax.

Try using a for loop to prevent code repetition. It might not be a recommended way though since there are so many variables involved.

Do you mean the for loop provided by Liquid template? Iteration – Liquid template language

yes. This should do.

OK. I’ll have a try. Thanks!

I find it’s not a problem of loop. I need to write something like <token>1</token>, which can automatically convert to <span style="font-size: 130%">&langle;</span>token<sub>1</sub><span style="font-size: 130%">&rangle;</span> for me. Is this possible?

You can use the Hugo shortcode style as mentioned here.

This is part of the markdown:

<div style="text-align: center">
  <code style="background: none">\expandafter</code> 
  {% include token.html content="e" %}
  {% include token.html content="1" %}
  {% include token.html content="2" %}
  <code style="background: none">...</code>
  {% include token.html content="n" %}
  <code style="background: none">...</code>
</div>

then the following rules describe the execution of `\expandafter`:

1. {% include token.html content="e" %} , the token immediately following `\expandafter`, is saved without expansion.

And this is token.html in _includes folder:

<span style="font-size: 130%;">&langle;</span>token<sub>{{ include.content }}</sub><span style="font-size: 130%;">&rangle;</span>

However, the output is weird:

The &langle; and &rangle; in div are shown as expected but those in the list have not been converted normally. How to fix this?

Now the workaround is to use the unicode character directly in token.html. But I don’t know why the strange bug happens.

I am also not sure about this. Seems to be an issue with liquid unable to parse HTML entities as per this comment.

Thanks. This is helpful!

1 Like

KaTeX supports macro definitions and \verb, so you could avoid repetition by defining (e.g.):

\gdef\tokensub#1{\langle\text{token}_{#1}\rangle}

then embed macro calls in math blocks in Markdown files:

\verb'\expandafter'\ \tokensub{e}\ \tokensub{1}\ \tokensub{2}\\ \ldots \ \tokensub{n}\ \ldots

Inputting the above on the KaTeX home page produces: