Right align (an address) in Pandoc - css

Right alignment comes handy for letter addresses. Unfortunately, according to the author John MacFarlane:
The pandoc document model does not allow for alignment other than in tables, so the alignment information is simply ignored.
We can resort to raw LaTeX.
Upon trial and error (and googling) I found that this TeX syntax works as intended:
\rightline{Address 1}
\rightline{Address 2}
\rightline{etc}
Also the LaTeX equivalent does:
\begin{flushright}
Address 1\\
Address 2\\
etc
\end{flushright}
The less invasive syntax, based on the LaTeX command raggedleft, unfortunately does not work:
{\raggedleft
Address 1\\
Address 2\\
etc\\
}
This is because braces are passed verbatim (\{, \}) and not as raw LaTeX.
With reference to centre alignment, I found a user claiming that markdown supports this native (beautiful) syntax:
-> This is center align <-
but I didn't find an editor/converter supporting it.
Indeed my working examples are all against the John Gruber markdown philosophy:
A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions.
Can you find a better way?
Particularly is it possible to simply write a Pandoc template to redefine the alignment of a header, say: ###### Right aligned?

For those that are looking for a similar question here's how to left align/right align on the same line:
**Some bolded text** **\hfill some right aligned on same line bolded text**
You'll have to imagine the result since I'm not sure how to recreate it on SO.

The best way to handle this is to put the address in a YAML metadata block (supported by pandoc 1.12+), and do the alignment in a custom template. Then your source document will be clean, without raw LaTeX, and you can modify templates to get the desired look in any output format.
Source document could look like this:
---
address:
| 23 Main St.
| Somewhere, MT 23434
...
Dear so and so,
Then in your LaTeX template you'd have something like:
\begin{flushright}
$address$
\end{flushright}
If you're writing a letter, though, it would probably be better to write a template using the LaTeX letter class.

Related

Xaringan: Math renders as inline code in footnote class

Using the default Xaringan CSS, if $math$ is placed in a footnote, it appears to be rendered as inline code, rather than rendering as math. I was not able to find information about this - is this just a natural limitation in Xaringan or remark.js? Or perhaps I am doing something wrong?
See basic example below:
# A Test
- This is a test<sup>1</sup>
.footnote[<sup>1</sup> This includes $\delta+\frac{2}{3}$ math.]
This will render the math between the $$ as if it were between backticks.
[UPDATE 17/12/2019] Another way to circumvent this is to replace the maths expression $\delta+\frac{2}{3}$ with \(\delta+\frac{2}{3}\) which bypasses any need for remark.js to convert $ and thus avoiding the bug there.
Yes I noticed this too. I think somehow remark.js is interfering with mathjax but I don't really know. To get around this I use the html code directly instead. So below should work:
# A Test
- This is a test<sup>1</sup>
<span class="footnote"><sup>1</sup> This includes $\delta+\frac{2}{3}$ math.</span>
[UPDATE]
The explanation of why this happens can be found here. TL;DR: remark.js removes the brackets necessary for mathjax to detect the maths.
Yihui also provided an alternate hack if the use of .footnote[] is desirable which involves adding the brackets to the inline math code:
.footnote[<sup>1</sup> This includes `\\(\delta+\frac{2}{3}\\)` math.]

RStudio: Code sections and compiled notebooks

I really like RStudio's code sections because they help me keep my code organized and makes navigation very easy. So, the code/comment line for code sections look like this (the key for RStudio to recognize the code section is that the comment should end with four or more #, - or =).
# Section One ---------------------------------
# Section Two =================================
### Section Three #############################
Now, I also find very useful "Compiling Notebooks from R Scripts" because you can produce reports directly from your script (which I find much more comfortable working with) without having to write an RMarkdown document. You simply have to use roxygen2-style comments (using #' as prefix) and they will be converted to markdown and/or use #+ or #- prefixes to control RMarkdown chunk options.
Now, I want the best of both worlds but I cannot find a way to get it. I want to have code sections (recognizable for RStudio) in the report (with the proper markdown for headings #, but without the extra #### at the end of the line). Seems like an impossible task to me. If I use #' prefix, I get the section in the report but RStudio does not recognize it as a code section. If I do not use roxygen2-style comments, RStudio recognizes the code section but it does not appear in the report (ok, it appears but as an ugly comment in the formatted code). The best solution I have found so far is to use the #+ prefix and simulate a code chunk label. That way I can get RStudio to recognize it as a code section, and it does not appear in the report as an ugly comment.
Any ideas on how to do this better?

How can knitr keep warning messages inside the box?

I'm writing a tutorial using knitr and I want to show some of the warnings and errors that students might encounter. While I'm capable of nicely displaying the code chunks within the box using the tidy=TRUE option, I don't understand how to handle the display of warnings and errors. For example, if I have the following code:
\documentclass{article}
\begin{document}
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
#
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
#
\end{document}
The warning code line is nicely displayed within the box, but the warning itself stretch beyond the box. I have a feeling that it has to do with the fact that the warning message is one very long string, but I don't know how to tell knitr to keep the warning inside the box. I've looked at the documentation on knitr chunk options and this formatR info, but I can't find the solution.
Thanks!
LaTeX is trying to generate a left and right justified block of text here. That means word-wrapping and hyphenating to get a nice straight right edge. Your warning has long words in it, LaTeX doesn't hyphenate typewriter text, so it overfills the box allocated for it and prints an overfull warning to the TeX log file.
Even if it could hyphenate the text, it might struggle finding a good place to hyphenate an odd word. For example, you should never break "buttoned" across lines as "but-toned". TeX has a complicated algorithm for this.
A solution may be to set \raggedright for your R blocks:
{
\raggedright
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
#
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
#
}
Like this, TeX should start a new line whenever a word would go outside the box. Enclose in a curly-bracket pair so normal text is unaffected. I don't know what else this might affect within the code block, so caveat emptor.

Mathematica-like (LaTeX) typesetting for own CAS application

As I am using Mathematica a lot I got the idea to write a small and free CAS which just exposes a very small subset of necessary functions and packages to be used and I want to present the results in an appropriate way to the user like Mathematica does (ignore the Facebook logo in the background :D ):
My first idea was to create LaTeX code in the background and to pdflatex the source and include the PDF then in the view... however this seems way to much overkill! I want to write this CAS either in C++ or C# and I want to know if there are any recommended solutions to output nice formula like that.
My first thought was a "real-time formula editing view" but it would be ok to have an input box to enter the commands and formulas and the upper view just to be uneditable output.
A few ways come to my mind.
Use LaTeX behind the scenes to typeset equations, as you say. Again, Cadabra does this.
Use TeXmacs as the front end. Cadabra does this.
Use MathJax. This is a javascript framework which renders TeX equations to images or MathML. It's very easy to use it if you have a HTML view in your UI toolkit. MathJax is used in the sister site MathOverflow, for example.
I find the route 3 is the most attractive.
For calling LaTeX in the background, don't use pdflatex, but use the non-PDF latex to produce a DVI file, and convert it then to PNG with dvipng.
Have a look at the preview package or the standalone class to get the output in the right size (i.e. only the formula, not a whole page).

Referencing figures with numbers in Sphinx and reStructuredText

When writing RST that will be processed with Sphinx, I can't get Sphinx LaTeX output to use figure numbers when referencing figures. For instance, this code:
The lemmings are attacking, as can be seen in :ref:`figlem`.
.. _figlem:
.. figure:: _static/lemming_invasion.*
They're coming!
Will be converted into this:
The lemmings are attacking, as can be
seen in They're coming!
/image goes here/
Figure 1.1: They're coming!
But what I want is the "standard" LaTeX way of referencing figures, like this:
The lemmings are attacking, as can be
seen in Figure 1.1
How do I achieve this? The code I'm currently using is what the Sphinx manual recommends, but it doesn't produce the output I want.
In the latest versions of Sphinx (1.3+), numbering figures and referencing them from text got a bit easier as support for it is now built-in.
In your text, you can do something like:
.. _label:
.. figure:: images/figure.*
At :numref:`label` you can see...
The end result should be something like "At Fig 1.1 you can see...". This technique works both with the default HTML output and the LaTeX output.
In your conf.py file, make sure to set the flag numfig = True. There are also configuration options for the references' text format (numfig_format and numfig_secnum_depth).
References:
http://www.sphinx-doc.org/en/stable/config.html#confval-numfig
https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#cross-referencing-figures-by-figure-number
The numfig extension does exactly this. I tried it and it worked for me.
To expand on the accepted answer, you can quickly get this set up as follows. Put the numfig.py file in your source directory. Then open conf.py and uncomment the line that says
sys.path.insert(0, os.path.abspath('.'))
Then add 'numfig' to the extensions list.
To use in your rst document, first label your figure (e.g., fig-main):
.. _fig-main:
.. figure:: main.png
This is the figure caption.
Finally, you can reference its figure number using the :num: directive, like this:
Refer to the main figure (Figure :num:`fig-main`).
I think referencing Figures is not yet implemented in reST, but here is workaround http://article.gmane.org/gmane.text.docutils.user/5623 that gets you closer.
One can use raw latex code, inline. For the example above, a role for raw latex code is first defined and than used to refer to the figure with the \ref{} latex command, and to set a label to the figure with the \label{} latex command.
The following should work:
.. role:: raw-latex(raw)
:format: latex
The lemmings are attacking, as can be seen in :ref:`figlem`
on figure :raw-latex:`\ref{pic:lem}`.
.. _figlem:
.. figure:: _static/lemming_invasion.*
They're coming! :raw-latex:`\label{pic:lem}`
Note that the \label{} command will appear inside the caption in the tex file, but it is still acceptable, at least by pdflatex. Also note that there should be at least one space before :raw-latex.

Resources