How to use Latex in dev.to editor? - latex

I wonder is there some way I could render math formulas in dev.to editor to show as mathematical equations in my articles?

It seems that Dev.to supports Katex for mathematical formulas.
From dev.to documents:
KaTeX Embed
Place your mathematical expression within a KaTeX liquid block, as follows:
{% katex %} c = \pm\sqrt{a^2 + b^2} {% endkatex %}
To render KaTeX inline add the "inline" option:
{% katex inline %} c = \pm\sqrt{a^2 + b^2} {% endkatex %}
Info taken also for dev.to github issues.

Related

Latex: \newcommand interpret # symbol problem

I defined a new command
\newcommand{\test}[1]{\href{https://github.com/microsoft/vscode/blob/main/package.json#1}{#1}}
When I use it as follows:
\test{#L4}
the url will be interpreted as:
https://github.com/microsoft/vscode/blob/main/package.json##L4
There is an extra # in the url, which is unexpected. What I really want is:
https://github.com/microsoft/vscode/blob/main/package.json#L4
which means line 4 of the package.json code
The easiest but no so elegant way to solve the problem is as follows.
\test{\#L4}
but what if other special characters like _ appear in the part of url I copy? It's boring to correct these meaningless grammar mistakes.
Is there any more elegant way to solve the problem? What I want is to copy plain text, which is part of url to the latex code and no extra efforts like adding \ escape character before # and other special characters are needed.
What I want is to copy plain text, which is part of url to the latex code and no extra efforts like adding \ escape character before # and other special characters are needed.
I am afraid it will require extra effort because # is used as a parameter for macros in LaTeX. You can play with redefining categories and when # is consumed, restore its original meaning.
I have another solution based on expl3 where I save the main address and create links with additional parts. See a small demo below
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[margin=1in]{geometry} % To fit long links without breaks
\ExplSyntaxOn
\str_new:N \l__xaddr_main_str
\str_new:N \l__xaddr_show_str
\cs_new:Npn \combine_addr:n #1 {
\str_set:Nn \l_tmpa_str {#1}
\str_concat:NNN \l__xaddr_show_str \l__xaddr_main_str \l_tmpa_str
}
\NewDocumentCommand\xsetmainaddr{v}{
\str_set:Nn \l__xaddr_main_str {#1}}
\NewDocumentCommand\xdisplay{v}{%
\str_if_empty:NF \l__xaddr_main_str {
\combine_addr:n {#1}
\str_use:N \l__xaddr_show_str
}}
\NewDocumentCommand\xhref{vv}{%
\str_if_empty:NF \l__xaddr_main_str {
\combine_addr:n {#1}
\href{\l__xaddr_show_str}{#2}
}}
\ExplSyntaxOff
\setlength\parindent{0pt} % To fit long links without breaks
\begin{document}
\xhref{#L4}{GitHub} % Nothing to display. The main address is not defined.
\xsetmainaddr{https://github.com/microsoft/vscode/blob/main/package.json}
\par\xdisplay{#L4} % Display the full address including #L4
\par\xdisplay{#something_else%20here} % all characters accepted
\par\xhref{#L4}{GitHub} % Generates link: <address>#L4
\bigskip
\xsetmainaddr{http://www.google.com} % New address
\par\xhref{}{Google} % Generates link
\end{document}

How to implement full LaTeX syntax in Rmarkdown?

I have a two part question regarding the use of LaTeX in Rmarkdown:
1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. \documentclass{...}, \begin{document}, \end{document}, etc.
I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.
2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{spmatrix}[1]
{\def\mysubscript{#1}\mathop\bgroup\begin{pmatrix}}
{\end{pmatrix}\egroup_{\textstyle\mathstrut\mysubscript}}
\begin{document}
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}
\end{document}
How would one implement this code in Rmarkdown? Do you need to move the \usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, \documentclass{article} even needed within an Rmarkdown document?
I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!
It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.
Concerning your concrete problem:
amsmath is already included by the default template, which also takes care of \ḑocumentclass and the document environment.
You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.
You can then use the LaTeX constructs as is in the Rmd body.
Putting things together:
---
output:
pdf_document:
keep_tex: yes
includes:
in_header: preamble.tex
---
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}

Pandoc: multiple footnotes next to one another?

If I want to have two footnotes cited next to one another, separated by a comma, what is the syntax for doing so? The Pandoc documentation doesn't seem to specify how.
As an example of what I'm trying to accomplish:
Some text here [^fn1, ^fn2] ## this clearly isn't the syntax, I've tried this.
becomes:
Some text here 1, 2.
The syntax for multiple footnotes would be:
Some text here [^fn1][^fn2]
[^fn1]: foo
[^fn2]: bar
However, to separate them by comma in PDF output, you'll have to tell LaTeX to do so by including the following in your pandoc template:
\usepackage[multiple]{footmisc}
For HTML output, you'd have to to something similar in CSS:
<style>
.footnote-ref ~ .footnote-ref :before {
content: ', '
}
</style>

Increment value in locomotivecms liquid markup

I wouldn't have come here if I hadn't tried many different approaches...
Obviously collections of objects do have a field size/length, but the components don't have an index. I want something like this:
{% for product in contents.products %}
<h3>Produkt {{ product.index + 1 }}</h3>
<p>{{ product.price | concat: ' €' }}</p>
{% endfor %}
I have tried the following as documented here:
http://www.omniref.com/ruby/gems/locomotivecms-liquid/classes/Liquid::Increment
{% increment variable %}
Doesn't work. I have to work in the backend editor which complains about bad syntax. Unknown tag increment. Could I be working with an old version? Unfortunately I can't check it.
I also tried assigning a value to 0 before the for loop:
{% assign x = 0 %}
And then manually increment it by 1:
{% assign x = x + 1 %}
There must be way! I mean this is basic programming. Has anybody found a way around this?
Thanks!
You can do increment in Locomotive CMS this way:
{% assign counter = 0 %}
{% capture counter %}{{ counter | plus: 1 }}{% endcapture %}

Jinja multiple rendering streams

Is there any way within a jinja template to render simultaneously to multiple streams?
Lets say I want to render a (printable) quiz, with first all the questions, then all the answers at the end. Each type of question (multiple choice, matching, missing word) is a different template.
Obviously I can take two passes and have question and answer in separate templates. But I would like to do this in one pass, so as to keep the templates well organised, and also to make the python calling code more regular, without a special case to handle this situation).
What I would like to have something like multiple 'streams', similar to blocks, but which which accumulate the output of multiple templates. Obviously the below is fantasy but is there another way within jinja to do this?
{% streams x, y %} {# define twp streams x and y #}
{% stream x %}
aaaa
{% endstream %}
{% stream y %}
bbbb
{% endstream %}
{% stream x %}
cccc
{% endstream %}
{% stream y %}
dddd
{% endstream %}
{{ x }} {# renders aaaacccc #}
{{ y }} {# renders bbbbdddd #}
Rendering to multiple streams is not possible. A simple solution would be to call the template twice, with a question boolean argument, and use if expressions to switch between question and answer code:
{% if questions %}
aaaa
{% else %}
bbbb
{% endif %}
{% if questions %}
cccc
{% else %}
dddd
{% endif %}
Then you call the template:
questions_html = template.render(questions=True)
answers_html = template.render(questions=False)

Resources