Rendering Angstrom in R Markdown - latex

I want to make the angstrom symbol in my r markdown document. Normally in latex you can just do \AA for the angstrom symbol. However, I am trying the simple $\AA$ in my r markdown file and it is not rendering. I have tried \AA{}, \Aa, \aa, and
$$
\AA
$$
as well with no luck.

UTF8 has the angstrom, the font is problematic though.
Another approach is to use overset.
These are not perfect as LaTeX, but still acceptable (at least for me).
$$1Å=10^{−10}m$$
$$1\overset{\circ}{A}=10^{-10}m$$

The rmarkdown package uses Pandoc, which in turn uses MathJax, to render math expressions in HTML. It is known that \AA does not work in MathJax (at least at this moment); see its issue #795, and you will also see alternative approaches there.

You can write the Unicode for angstrom, but you have to convert to decimal from hex. The hex for angstrom is 212B, so this is 8491 in decimal. Thus &#8491 will work

You could inline unicode into R Markdown using asis_output :
# Inlined unicode - Angstrom : `r knitr::asis_output('\U212B')`

I develop it to fit this
$\overset{\circ}{\mathbb{C}}$

Related

Writing Latex math expression in Maxima

How I can write Latex/TEX expressions in legend and labels of plot2d?
Thanks.
Maxima relies on Gnuplot for displaying plots, so to extent Maxima can handle TeX/LaTeX in labels and legends depends on what Gnuplot can handle.
Gnuplot has a LaTeX output format, but that is a file output format, not for display on the screen, so I won't consider it further here. But maybe there is some way to output a LaTeX file and then launch a viewer for it? I haven't investigated that possibility.
From looking at some discussions in the Gnuplot project, it appears that Gnuplot cannot handle TeX/LaTeX in output formats which are displayed directly to the screen. However, Gnuplot recognizes some escape sequences, and also allows Unicode characters in strings when the terminal option enhanced is specified. This allows some subset of LaTeX-like functionality.
About escape sequences, see "Enhanced text mode", pp 32--33 in the current version of the manual: http://gnuplot.info/docs_5.4/Gnuplot_5_4.pdf
About Unicode characters, see "Escape sequences", p 34. See also the examples: http://www.gnuplot.info/demo/enhanced_utf8.html and http://www.gnuplot.info/demo_5.4/unicode.html .
On looking at how Maxima interacts with Gnuplot, it appears that Maxima disabled the enhanced flag, so stuff like underscore and caret have no effect, and escape sequences are not recognized. EDIT: You can enable Gnuplot enhanced strings in Maxima via: set_plot_option([gnuplot_strings, true]);
When Unicode characters are in a string supplied by Maxima to Gnuplot, they are displayed correctly.
Most Lisp implementations handle Unicode characters; GCL is the exception.
I know this is not straightforward; this is a summary of what I could figure out, and I hope it helps.

Tilde over n when when converting from markdown to latex with pandoc

I have a markdown document that I convert to PDF via pandoc's latex engine. I'm trying to render an n with a tilde over it, as in "niño", with markdown like the following:
ni\~{n}o
...but this just gets rendered in the PDF as "ni~no" -- i.e. the tilde gets interpreted literally. I've also tried escaping the backslash (ni\\~{n}o), surrounding everything in brackets (ni{\~{n}}o), and basically what I think is every possible combination of escaping characters in this sequence, but nothing works. It also fails even when the sequence is on its own (i.e. \~{n}).
But, other similar sequences that are based on letters rather than symbols work just fine (e.g. Otter\r{a} gets rendered correctly to "Otterå"). Pandoc is specifically failing to handle the tilde (or maybe more generally non-letter-based latex character sequences -- I haven't tested others).
The command I'm using to build the pdf is pandoc file.md -o file.pdf. I've also tried specifying -f markdown+raw_tex, but it still fails (nor should I need to, since the \r{a} works without it, and I think raw_tex is enabled by default anyway).
Any thoughts? I know I can use xetex to just enter these characters directly, but that's not really a satisfying solution...
Besides using the ñ character directly (which apparently works in native Pandoc because it's magic!), an alternative is to create a simple LaTeX \newcommand for forcing native TeX interpretation.
\newcommand{\tex}[1]{#1}
ni\tex{\~n}o
Thanks to John McFarlane for introducing me to this clever workaround!

Physics bra-ket symbols in IPython

I am trying to have the ket symbol which is usually written in latex as \ket{\psi}. However, this doesn't work when written within the $$ ... $$. This also doesn't work when written using IPython.display.
$ latex
** \documentclass{article}\usepackage{amsmath}\begin{document}
... chatter chatter ...
* \show\ket
> \ket=undefined.
So your \ket macro is coming from some package or other which I don't know what it is. This doesn't explain why it doesn't work in $$ ... $$ -- you have to work at it to get things to not work in display math -- but it does explain why it doesn't work in IPython: whichever package this is isn't being loaded in IPython's canned TeX environment (or) IPython's emulation of TeX math input language does not include this macro.
Here's a reasonable definition in terms of primitive math symbols, which should work fine in display mode. I don't know if you can put this into IPython verbatim, but you should at least be able to write out the expansion longhand when needed (e.g. $$ \left| \psi \right\rangle $$)
\newcommand{\ket}[1]{\left|{#1}\right\rangle}
\newcommand{\bra}[1]{\left\langle{#1}\right|}
EDIT: define macros using LaTeX preferred interface, rather than primitive \def.
I was able to extend #zwol 's answer to create a \braket command as well for use in a Jupyter notebook:
$$\newcommand{\braket}[2]{\left\langle{#1}\middle|{#2}\right\rangle}$$
$$\braket{\Psi^*}{\Psi}$$
$$\braket{\frac{\Psi^*}{2}}{\Psi}$$
Gives:
IPython uses MathJax to render LaTeX in the notebook. MathJax is great but only supports a subset of LaTeX hence there are some limitations. See MathJax homepage for details.
A new command like proposed by #Zack will work fine though. Be aware that such custom commands will likely lead to some issues when converting to LaTeX (nbconvert).

Icelandic, utf8 and utf8x in LaTeX

First of all, what's the difference between utf8 and utf8x in
\usepackage[utf8]{inputenc}
\usepackage[utf8x]{inputenc}
when used in LaTeX?
Secondly, what packages are required when writing an article in Icelandic using LaTeX? I found:
\usepackage[icelandic]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
after experimenting a bit but I have a feeling some part of the code may be redundant. And even with them the aforementioned packages, the code inside
\begin{lstlisting}
...
\end{lstlisting}
isn't rendered with Icelandic characters when outputted through pdflatex in Ubuntu, although it works on my friend's computer (who's running Debian). What's missing?
[UTF8] is "supported" by the LaTeX team and covers a fairly specific/limited range of unicode input characters. It only defines those symbols that are known to be available with the current font encoding.
[UTF8x], AFAIK, is no longer supported, but covers a much broader range of input symbols. I would recommend only trying it if [UTF8] doesn't do what you need.
Secondly, the listings package (and most other related packages that do character scanning) does not support UTF8 input. (If it's working on a friend's machine they must be using an 8-bit input encoding instead.) The listingsutf8 package provides a UTF8-compatible replacement for \lstinputlisting but not for the main lstlisting environment. Using XeLaTeX might help you here, however.

Is there a calculator with LaTeX-syntax?

When I write math in LaTeX I often need to perform simple arithmetic on numbers in my LaTeX source, like 515.1544 + 454 = ???.
I usually copy-paste the LaTeX code into Google to get the result, but I still have to manually change the syntax, e.g.
\frac{154,7}{25} - (289 - \frac{1337}{42})
must be changed to
154,7/25 - (289 - 1337/42)
It seems trivial to write a program to do this for the most commonly used operations.
Is there a calculator which understand this syntax?
EDIT:
I know that doing this perfectly is impossible (because of the halting problem). Doing it for the simple cases I need is trivial. \frac, \cdot, \sqrt and a few other tags would do the trick. The program could just return an error for cases it does not understand.
WolframAlpha can take input in TeX form.
http://blog.wolframalpha.com/2010/09/30/talk-to-wolframalpha-in-tex/
The LaTeXCalc project is designed to do just that. It will read a TeX file and do the computations. For more information check out the home page at http://latexcalc.sourceforge.net/
The calc package allows you to do some calculations in source, but only within commands like \setcounter and \addtolength. As far as I can tell, this is not what you want.
If you already use sage, then the sagetex package is pretty awesome (if not, it's overkill). It allows you get nicely formatted output from input like this:
The square of
$\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}$
is \sage{matrix([[1, 2], [3,4]])^2}.
The prime factorization of the current page number is \sage{factor(\thepage)}
As Andy says, the answer is yes there is a calculator that can understand most latex formulas: Emacs.
Try the following steps (assuming vanilla emacs):
Open emacs
Open your .tex file (or activate latex-mode)
position the point somewhere between the two $$ or e.g. inside the begin/end environment of the formula (or even matrix).
use calc embedded mode for maximum awesomeness
So with point in the formula you gave above:
$\frac{154,7}{25} - (289 - \frac{1337}{42})$
press C-x * d to duplicate the formula in the line below and enter calc-embedded mode which should already have activated a latex variant of calc for you. Your buffer now looks like this:
$\frac{154,7}{25} - (289 - \frac{1337}{42})$
$\frac{-37651}{150}$`
Note that the fraction as already been transformed as far as possible. Doing the same again (C-x * d) and pressing c f to convert the fractional into a floating point number yields the following buffer:
$\frac{154,7}{25} - (289 - \frac{1337}{42})$
$\frac{-37651}{150}$
$-251.006666667$
I used C-x * d to duplicate the formula and then enter embedded mode in order to have the intermediate values, however there is also C-x * e which avoids the duplication and simply enters embedded mode for the current formula.
If you are interested you should really have a look at the info page for Emacs Calc - Embedded Mode. And in general the help for the Gnu Emaca Calculator together with the awesome interactive tutorial.
You can run an R function called Sweave on a (mostly TeX with some R) file that can replace R expressions with their results in Tex.
A tutorial can be found here: http://www.scribd.com/doc/6451985/Learning-to-Sweave-in-APA-Style
My calculator can do that. To get the formatted output, double-click the result formula and press ctrl+c to copy it.
It can do fairly advanced stuff too (differentiation, easy integrals (and not that easy ones)...).
https://calculator-algebra.org/
A sample computation:
https://calculator-algebra.org:8166/#%7B%22currentPage%22%3A%22calculator%22%2C%22calculatorInput%22%3A%22%5C%5Cfrac%7B1%2B2%7D%7B3%7D%3B%20d%2Fdx(arctan%20(2x%2B3))%22%2C%22monitoring%22%3A%22true%22%7D
There is a way to do what you want just not quite how you describe.
You can use the fp package (\usepackage[options]{fp}) the floating point package will do anything you want; solving equations, adding dividing and many more. Unfortunately it will not read the LaTeX math you instead have to do something a little different, the documentation is very poor so I'll give an example here.
for instance if you want to do (2x3)/5 you would type:
\FPmul\p{2}{3} % \p is the assignment of the operation 2x3
\FPupn\p{\p{} 7 round} % upn evaluates the assignment \p and rounds to 7dp
\FPdiv\q{\p}{5} % divides the assigned value p by 5 names result q
\FPupn\q{\q{} 4 round} % rounds the result to 4 decimal places and evaluates
$\frac{2\times3}{5}=\FPprint\q$ % This will print the result of the calculations in the math.
the FP commands are always ibvisible, only FPprint prints the result associated with it so your documents will not be messy, FP commands can be placed wherever you wish (not verb) as long as they are before the associated FPprint.
You could just paste it into symbolab which as a bonus has free step by step solutions. Also since symbolab uses mathquill it instantly formats your latex.
Considering that LaTeX itself is a Turing-complete markup language I strongly doubt you can build something like this that isn't built directly into LaTeX. Furthermore, LaTeX math matkup itself has next to no semantic meaning, it merely describes the visual appearance.
That being said, you can probably hack together something which recognizes a non-programmable subset of LaTeX math markup and spits out the result in the same way. If all you're interested in is simple arithmetics with fractions and integers (careful with decimal fractions, though, as they may appear as 3{,}141... in German texts :)) this shouldn't be too hard. But once you start with integrals, matrices, etc. I fear that LaTeX lacks expressiveness to accurately describe your intentions. It is a document preparation system, after all and thus not very suitable as input for computer algebra systems.
Side note: You can switch to Word which has—in its current version—a math markup language which is sufficiently LaTeX-like (by now it even supports LaTeX markup) and yet still Google-friendly for simpler terms:
With the free Microsoft Math add-in you can even let Word calculate expressions in-place:
There is none, because it is generally not possible.
LaTeX math mode markup is presentational markup and there are cases in which it does not provide enough information to calculate the expression.
That was one of the reasons MathML content markup was created and also why MathML is used in Mathematica. MathML actually is sort of two languages in one:
presentation markup
content markup
To accomplish what you are after you'll have to have MathML with comibned presentation and content markup (see MathML spec).
In my opinion your best bet is to use MathML (even if it is verbose) and convert to LaTeX when necessary. That said, I also like LaTeX syntax best and maybe what we need is a compact syntax for MathML (something similar in spirit to RelaxNG compact syntax).
For calculations with LaTeX you can use a CalcTeX package.
This package understand elements of LaTeX language and makes an calculations, for example your problem is avialble on
http://sg.bzip.pl/CalcTeX/examples/frac.tgz
or just please write
\noindent
For calculation please use following enviromentals
$515.1544 + 454$
or
\[ \frac{154.7}{25}-(289-\frac{1337}{42.})
\]
or
\begin{equation}
154.7/25-(289-1337/42.)
\end{equation}
For more info please visite project web site or contact author of this project.
For performing the math within your LaTeX itself, you might also look into the pgfmath package, which is more powerful and convenient than the calc package. You can find out how to use it from Part VI of The TikZ and PGF Packages Manual, which you can find here (version 2.10 currently): http://mirror.unl.edu/ctan/graphics/pgf/base/doc/generic/pgf/pgfmanual.pdf
Emacs calc-mode accepts latex-input. I use it daily. Press "d", followed by "L" to enter latex input mode. Press "'" to open a prompt where you can paste your tex.
Anyone saing it is not possible is wrong.
IIRC Mathematica can do it.
There is none, because it is generally not possible. LaTeX math mode
markup is presentational markup and there are cases in which it does
not provide enough information to calculate the expression.
You are right. LaTeX as it is does not provide enough info to make any calculations.Moreover, it does not represent any information to do it. But nobody prevents to wright in LaTeX format a text that contains such an information.
It is a difficult path, because you need to build a system of rules superimposed on what content ofthe text in Latex format needs to contain that it would be recognizable by your interpreter. And then convince the user that it is necessary to learn, etc. etc...
The easiest way to create a logical and intuitive calculator of mathematical expressions. And the expression is already possible to convert latex. It's almost like what you said. This is implemented in the program which I have pointed to. AnEasyCalc allows to type an expression as you type the plane text in any text editor. It checks, calculates and generate LateX string by its own then. Its very easy and rapid work. Just try and you will see that.
This is not exactly what you are asking for but it is a nice package
that you can include in a LaTeX document to do all kind of operations including arithmetic, calculus and even vectors and matrices:
The package name is "calculator"
http://mirror.unl.edu/ctan/macros/latex/contrib/calculator/calculator.pdf
The latex2sympy2 Python library can parse LaTeX math expressions.
from latex2sympy2 import latex2sympy
tex_str = r"""YOUR TEX MATH HERE"""
tex_str = r"\frac{9\pi}{\ln(12)}+22" # example TeX math
sympy_object = latex2sympy(tex_str)
evaluated_tex = float(sympy_object.evalf())
print(evaluated_tex)
This Python 3 code evaluates 9𝜋/ln(12)+22 (in its LaTeX from above) to 33.37842899841745.
The snippet above only handles basic algebraic simplification (math expressions without variables). Since the library converts LaTeX math to SymPy objects, the above code can easily be tweaked and extended to handle much more complicated LaTeX math (including solving derivatives, integrals, etc...).
The latex2sympy2 library can be installed via the pip command: pip install --user latex2sympy2
<>
try the AnEasyCalc program. It allows to get the latex formula very easy:
http://steamandwater.od.ua/AnEasyCalc/
:)

Resources