Markdown and references inside the listing captions - latex

Is it possible to process the text markdown for the listing captions in pandoc?
Pandoc works well for figure captions and translates the emphasis and inline code, but apparently fails to do so in listing captions.
For example, in the code below, I have some formatting and reference in the captions for the second listing and the figure.
However, only the formatting in the figure is correctly transformed into latex.
The listing caption is left as it is, which breaks the tex processer later in the pipeline.
Is it possible to process the listing captions just like the figure captions?
```{#lst:first .C caption="Hi"}
int hi() {
return ((int)'h'<<8) | 'i';
}
```
```{#lst:second .C caption="Code *using* the function `hi` from [#lst:first]"}
x = hi();
```
![Picture *with* `inline code` and reference [#lst:first]](picture.png)
pandoc example.so.md -o example.so.tex --listings --filter=pandoc-crossref
Produces:
\begin{codelisting}
\caption{Hi}
\begin{lstlisting}[language=C, caption=Hi, label=lst:first]
int hi() {
return ((int)'h'<<8) | 'i';
}
\end{lstlisting}
\end{codelisting}
\begin{codelisting}
\caption{Code *using* the function `hi` from {[}#lst:first{]}}
\begin{lstlisting}[language=C, caption={Code *using* the function `hi` from [#lst:first]}, label=lst:second]
x = hi();
\end{lstlisting}
\end{codelisting}
\begin{figure}
\centering
\includegraphics{picture.png}
\caption{Picture \emph{with} \passthrough{\lstinline!inline code!} and
reference lst.~\ref{lst:first}}
\end{figure}
I use pandoc 2.7.3 and pandoc-crossref v0.3.4.1
P.S. as https://github.com/jgm/pandoc/issues/673 suggests, there may still be no native support for this. Is there a workaround?

K4zuki's response on the pandoc google group worked for me.(https://groups.google.com/forum/#!msg/pandoc-discuss/DItTuL5S1EM/L1Ou25gTCAAJ)
Use the option for pandoc-crossref explained here: http://lierdakil.github.io/pandoc-crossref/#table-style-captions
add codeBlockCaptions: true in your metadata block or run pandoc with -M codeBlockCaptions=true
This worked:
Listing: Code *using* the function `hi` from [#lst:first]
```{#lst:second .C}
x = hi();
```

Related

Latex to HTML with Pandoc, how do I include the Lua script output to the conversion?

I'm using pandoc to convert LaTeX to HTML. However, I have a lua script included in the latex file (which pulls some data from a JSON file and formats the data to LaTeX). As I convert to HTML, the script is not executed but appears as lua in the output.
Is there either a way to get a pure latex output for the conversion or to run the script during the conversion?
Unfortunately, the answer is "yes, but actually: no".
What I mean is that you can run the Lua code, but it is very likely to contain code which is luatex specific and will not work in pandoc.
Let's look at an example:
\documentclass{article}
\usepackage{luacode}
\begin{document}
You are runnig:
\begin{luacode}
tex.print(_VERSION)
\end{luacode}
\end{document}
The script, when run through lualatex, will report the Lua version used to execute the code (currently "Lua 5.3"). The tex.print command is provided by lualatex.
To see how pandoc handles this, we can convert it into pandoc's internal format with pandoc --to=native. Pandoc doesn't know the luacode environment, so it treats it like normal text.
[Para [Str "You",Space,Str "are",Space,Str "runnig:"]
,Div ("",["luacode"],[])
[Para [Str "tex.print(_VERSION)"]]]
We see that the block becomes a div with class luacode. It's possible to run a Lua filter and execute it's content:
-- file: run-luacode.lua
function Div(d)
local code = pandoc.utils.stringify(d)
load(code)()
end
Using this with
pandoc my-test.latex --to=html --lua-filter=run-luacode.lua
Will lead to an error, because tex.print is undefined in pandoc's Lua.
Error running filter run-luacode.lua:
[string "tex.print(_VERSION)"]:1: attempt to index a nil value (global 'tex')
stack traceback:
[string "tex.print(_VERSION)"]:1: in main chunk
run-luacode.lua:3: in function 'Div'
Of course, we could define tex.print in the pandoc filter. E.g., setting
tex = {['print'] = print}
will at least print the result to the console. You could devise a mechanism that actually converts it to pandoc's internal document format. See https://pandoc.org/lua-filters.html for details.
It might also be beneficial to call pandoc with --from=latex+raw_tex, which makes pandoc keep the unknown luacode environment verbatim in a RawBlock element. This can be easier to process in the filter.

Can anyone tell me something about the doxygen.sty?

I'm using Doxygen to generate PDF document. So I need also make use of LaTeX. I generated the default LaTeX style file, i.e. the doxygen.sty. I want to use the latex package listings to make the code block (wrapped by Doxygen commands #code and #endcode) look better, but when I made changes to this part, LaTeX errosr would appear and no PDF file can be output successfully. Am I wrong somewhere? What's the correct way to add new style in the doxygen.sty? Thanks in advance!
Below are default content in doxygen.sty:
% Used by #code ... #endcode
\newenvironment{DoxyCode}{%
\par%
\scriptsize%
\begin{alltt}%
}{%
\end{alltt}%
\normalsize%
}
Below are changes I made for the DoxyCode environment:
\RequirePackage{listings}
\RequirePackage[table]{xcolor}
...
% Used by #code ... #endcode
\newenvironment{DoxyCode}{%
\par%
\lstset{numbers=left,numberstyle=\tiny}%
\scriptsize%
\begin{lstlisting}[language=C]%
}{%
\end{lstlisting}%
\normalsize%
}

change mathjax renderer in R notebooks (with "self_contained: false")

I am creating R notebooks that contain equations. I am using RStudio 1.2.5033 on Windows 10, R 3.5.1, and rmarkdown 2.1. When my R notebooks are rendered as HTML, MathJax (v2.7.2) uses the "HTML-CSS" output processor to render the equations. But I think that the output from the "CommonHTML" output processor looks better. So I want to include a directive, in my R notebooks, that forces MathJax to use the CommonHTML output processor. How may I do this?
If I were rendering an ordinary R Markdown document with output format html_document, I could solve the problem via the mathjax option in my YAML header. For example, when the following file is rendered to HTML, MathJax will use the CommonHTML output processor:
---
title: "Trouble with MathJax"
output:
html_document:
mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
self_contained: false
---
\begin{equation}
R_3 = \alpha
\end{equation}
But this solution doesn't work well when I change the output format from html_document to html_notebook. In that case, I get output that looks like this:
The equation is rendered with CommonHTML, but there is a lot of cruft at the top of the page (note the four bullet points), and the default R Notebook CSS doesn't seem to be implemented.
The problem seems to be general to rendering R notebooks with self_contained: FALSE, as suggested in R notebooks don't render properly when "self_contained" is FALSE because the "files" directory is deleted after rendering. But I can't see a good workaround for that problem.
Dead Ends
The MathJax documentation seems to indicate that I can specify the output processor by adding the jax array in a call to MathJax.Hub.Config(). But when I've done that, my equations are still displayed via the HTML-CSS output processor. Here is a minimal example of an R Markdown document that exhibits the problem:
---
title: 'Trouble with MathJax'
output: html_notebook
---
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
jax: ["input/TeX", "output/CommonHTML"],
});
</script>
\begin{equation}
R_3 = \alpha
\end{equation}
The call to MathJax.Hub.Config() seems to do nothing here. In both Chrome and Edge, the equation is rendered via HTML-CSS, not CommonHTML. How can I change the rendering to Common HTML?
Related Posts
One year-old post, Is there a way in markdown to override default mathjax renderer?, is about Jupyter notebooks, but it's relevant. It hasn't received an answer.
Adapting the script in this post from the MathJax Google Group –– mainly by changing "HTML-CSS" to "CommonHTML" –– doesn't seem to have any effect.
The solution is simply to omit the self_contained line in the YAML header or, equivalently, to set self_contained to true. Here is a minimal example of an R notebook for which the user has chosen the mathjax renderer:
---
title: "Self-contained notebook with non-default Mathjax config"
output:
html_notebook:
mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
---
$R_3 = 2$.
When the file is rendered to HTML, the equation is displayed with CommonHTML, not with HTML-CSS. And the Mathjax script is contained within the "nb.html" file that is produced.
I was surprised that this works, because the documentation for rmarkdown::html_document() says that "even for self contained documents MathJax is still loaded externally (this is necessary because of its size)." But Section 3.1.8 of the R Markdown book indicates that the restriction applies only when Mathjax is loaded from a local file. So perhaps it shouldn't be a surprise.
Side note: the default Mathjax configuration used by the rmarkdown package is given by rmarkdown:::mathjax_config(). As of rmarkdown v2.1, the function returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".

Problem with creating a newenvironment in LaTeX

I am trying to implement this new environment in LaTeX:
\newenvironment{javacode}[2]
{\begin{lstlisting}[language=java, label=#1, caption=#2]}
{\end{lstlisting}}
And then use it like such:
\begin{javacode}{c}{some code}
int x = 5;
\end{javacode}
But I am getting the following error:
Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][]
[1] [2]) [3])
*
Can anyone help as regards fixing this problem?
[Update]
I tried it doing it like Red-nosed unicorn instructed, and it worked correctly.
But now I tried adding a \begin{singlespace} like such:
\lstnewenvironment{javacode}[2]
{
\begin{singlespace}
\lstset{language=java, label=#1, caption=#2}}
{
\end{singlespace}
}
And I got the same error:
Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][]
[1]) [2] [3])
*
This is a special case because the listings environment needs to parse ahead itself to find the end of itself. The reason is that macros inside the listings environment must not get expanded – that of course includes the end tag of the environment.
So basically it looks in each line if the line contains \end{lstlisting} – but in your case, no such line exists since the \end{javacode} macro has not yet been expanded. So listings continues to search until the end of the file.
Listings defines an own command to work around this. From the documentation:
\lstnewenvironment
{⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
{⟨starting code⟩}
{⟨ending code⟩}
For example:
\lstnewenvironment{javacode}[2]
{\lstset{language=java, label=#1, caption=#2}}
{}
EDIT In response to your edited question: I tried to compile the following minimal “working” example. Actually, it’s not so much working – the latex processor just stops right in the middle and waits for a user input.
Since the listings documentation makes no mention of a special treatment of singlespace, I think you may have uncovered a bug. The best course of action is probably to get feedback from the maintainer of the listings package.
% mini.dvi
\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\doublespacing
\lstnewenvironment{javacode}
{\begin{singlespace}
\lstset{language=java}}
{\end{singlespace}}
\begin{document}
\begin{javacode}
int a = 1;
int b = 2;
\end{javacode}
\end{document}
Upon further research, I found this http://www.tug.org/pipermail/texhax/2009-June/012699.html
To workaround my solution, I need to use \singlespacing instead of the singlespace environment.
The following is now my working code:
\lstnewenvironment{javacode}[2]
{\singlespacing\lstset{language=java, label=#1, caption=#2}}
{}

LaTeX semiverbatim and fancyvrb

I'm creating a presentation using the beamer LaTex package. Beamer
comes with an environment called "semiverbatim" which is like
"verbatim", but allows you to place commands inside the environment.
This is useful in beamer to control how the overlays of a frame
unfold. For example:
\begin{frame}[fragile, shrink]
\frametitle{Some Code Sample}
\begin{semiverbatim}
private String foobar() \{
String s = "val"
\alert<2->{s = null};}
return s;
\}
\end{semiverbatim}
\end{frame}
This will cause the third line to appear red in the second stage of
the frame transition.
This is all good and fine, however, the "semiverbatim" environment,
much like the "verbatim" environment, is pretty limited. I would like
to use the "Verbatim" environment from the fancyvrb package.
Is there anyway to use "Verbatim" in the same way "semiverbatim" is
used?
I'm not having much luck, I'm afraid. I can get the \alert to work okay, but only without an overlay specification:
\documentclass{beamer}
\usepackage{fancyvrb}
\begin{document}
\begin{frame}[fragile]
\frametitle{Some Code Sample}
\begin{Verbatim}[commandchars={\\[]}]
private String foobar() {
String s = "val"
\alert[s = null];}
return s;
}
\end{Verbatim}
\end{frame}
\end{document}
When you try \alert<2-> it breaks, and changing catcodes of < and > doesn't seem to help.
Not sure if it helps you directly, but when I've loaded source into a beamer slide, I used the listings package, lstset, and the lstlisting environment. I never use any reveals in the code, though, so I haven't tested that interaction.

Resources