This might be an edge case, but I'm trying to create a beamer presentation with knitr where I want to display a code chunk using different formulas as arguments for a function. I found that when using overlays, the tilde in the code chunks disappear. Is there a way to get them to display?
Here's a minimal reproducible example:
\documentclass{beamer}
\begin{document}
\begin{frame}[fragile]
\frametitle{Slide with overlay}
\only<1>{
<<notilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
#
}
\only<2>{
<<notilde2, eval = FALSE>>=
myfunction(data, formula = y ~ x)
#
}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slide without overlay}
<<tilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
#
\end{frame}
\end{document}
The first frame of the presentation looks like this:
Any help is appreciated.
EDIT:
The slide without overlay looks like this:
To clarify, the syntax of <<...>> and # is specific to the R package knitr. Anything between these symbols are interpreted by knitr as R code chunks and converted to latex to include syntax highlighting.
You can replace the ~ with the math mode $\sim$
If not in a special environment like verbatim, the tilde in latex is a protected space, i.e. a space at which no line break occurs.
It is not clear from your question: is it working for your second frame, without overlay?
I don't know part of your syntax: is the << ... >>= ... # equivalent to a verbatim or code environmet? It looks a bit, as your text is set in a fixed space font and has syntax highlihting.
Related
In my latex document I am using the listings package extensively.
I have many short inline code snippets that I like to give a proper highlighting in the text, and I am using the \lstMakeShortInline construct. Now I am interested in highlighting (background coloring) at least some of those code inserts for clarity, and was trying the following:
\lstMakeShortInline[language=Python,basicstyle=\ttfamily, backgroundcolor=\color{lightgray}]!
hoping that usage like:
some text some text !read_csv()! some more text
Will result in read_csv() appearing on a light-gray background, but it does not seem to work. The \ttfamily format works well in this situation.
(Thanks to samcarter_is_at_topanswers.xyz)
Here is a minimal example:
\documentclass{book}
\usepackage{listings}
\usepackage{color}
\lstset{language=Python}
\begin{document}
\lstMakeShortInline[language=Python, keywordstyle={\bfseries \color{blue}}, backgroundcolor=\color{yellow}]!
Here is the keyword !for!, showing that the \textbf{keywordstyle} setting takes effect, but the \textbf{backgroundcolor} setting does not.
\textbf{backgroundcolor} fails to take effect also when applied to a non-keyword !df.read_csv()! code snippet.
\end{document}
Is this possible with the listings package?
Thanks
Michael
Based on https://tex.stackexchange.com/a/357239/36296 you could do something like (make sure that ! does not occur in the normal text or use a different character):
% !TeX program = lualatex
\documentclass{article}
\usepackage{xcolor,listings,realboxes,fancyvrb} % fancyvrb for '\Verb' macro
\definecolor{mygray}{rgb}{0.8,0.8,0.8}
\lstset{basicstyle=\ttfamily, breaklines = true, backgroundcolor=\color{mygray}}
\usepackage[doublespacing]{setspace} % just for this example
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
-- the following code employs Lua's powerful "string.gsub" function
function color_lstinline ( s )
s = string.gsub ( s , "%b!!", "\\Colorbox{mygray}{%0}" )
return s
end
\end{luacode}
%% Define 2 LaTeX macros to switch operation of Lua function on and off
\newcommand{\ColorLstinlineOn}{\directlua{
luatexbase.add_to_callback ( "process_input_buffer" ,
color_lstinline, "color_lstinline" )}}
\newcommand{\ColorLstinlineOff}{\directlua{
luatexbase.remove_from_callback ( "process_input_buffer" ,
"color_lstinline" )}}
\AtBeginDocument{\ColorLstinlineOn} % Default: activate the Lua function
\lstMakeShortInline[language=Python, keywordstyle={\bfseries \color{blue}}, backgroundcolor=\color{yellow}]!
\begin{document}
!for!
\end{document}
I'm having issues turning this word equation into a LaTex equation. It's coming out looking dodgy, please help!
I added a screen shot of the equation I want, and what I end up getting when I copy and paste into LaTex:
WORD:
LATEX CODE:
\mathrm{=\ }\mathrm{C}_\mathrm{0}\mathrm{[1-}6(Dt)1/2aπ2-3Dta2] + 12(Dt)1/2an = 1∞exp(na(Dt)1/2)
and therefore nothing comes out and LaTex doesn't let me run it.
This is absolutely not a proper LaTeX equation code. I don't know what you know about LaTeX, but you cannot just copy and paste from Word or any software to you LaTex editor. Plus, you need to provide your full code for anyone being able to help you.
Anyway, running this MWE should work :
\documentclass[11pt, a4paper, twoside]{report}
% ===== PACKAGES DECLARATION =====
\usepackage{mathtools} % Replaces amsmaths + more features
\usepackage{amsfonts} % Maths fonts package
% ===== DOCUMENT BODY =====
\begin{document}
\begin{equation} % optional : use the "equation*" environment to remove equation number
% optional : use traditional math font by removing the \mathrm{} command
\mathrm{X = C_0 \left[ 1 - \frac{6(Dt)^{1/2}}{a \pi^2} - \frac{3Dt}{a^2}\right] + \frac{12(Dt)^{1/2}}{a} \sum_{n=1}^\infty \exp\left(\frac{na}{(Dt)^{1/2}} \right)}
% optional : remove auto-sized brackets by removing the \left and \right commands
\end{equation}
\end{document}
As written in the code, you may want to remove the equation number and the big auto-sized brackets (that are more readable in my opinion). Just remove the corresponding commands. Also, you should consider using the "normal" math font and not the roman one that is clearly different from the text and helps the reader to separate equations from inline small expressions you could insert in your document.
One first sketch:
\documentclass{article}
\begin{document}
\[
C_0\left[1-\frac{6(Dt)^{\frac{1}{2}}}{a\pi^2}-\frac{3Dt}{a^2}\right]+%
\frac{12(Dt)^{\frac{1}{2}}}{a}\sum^{\infty}_{n=1}%
\exp\left(\frac{na}{(Dt)^{\frac{1}{2}}}\right)
\]
\end{document}
No packages required. The output:
Than you can tune the math fonts and anything else.
I would like to include some Julia source in a LaTeX doc. I am familiar with the 'listings' package, and have crafted a decent 'lstdefinelanguage'. But I am having trouble with non-ASCII characters in the jl. I have played around with various 'lstset' and 'inputenc' options like 'extendedchars' and 'utf8' to no avail. I am using TeXShop on a Mac. Has anyone cracked this?
You can use literate option although it requires some set-up work. Here is a MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{courier}
\lstset{basicstyle=\ttfamily}
\lstset{literate=
{δ}{{$\delta$}}1
{ϵ}{{$\epsilon$}}1
}
\begin{document}
\begin{lstlisting}
function f()
ϵϵϵϵϵ = 1
xxxxx = 3
δδδδδ = 2
ϵ + x + δ
end
\end{lstlisting}
\end{document}
I would like to place the caption above the figure using knitr in texmaker.
I know that this question has already been asked, and I understand that the solution suggested so far is to use:
\begin{figure}
\caption{This is a caption above the figure}
<<a-plot, echo=FALSE>>=
plot(1)
#
\end{figure}
But in this way I cannot show the code (since echo=FALSE).
And if I choose instead echo=TRUE, what I get is the caption, then the codes, and then the graph, which is also not what I want.
What I would like to show is the code for R, (and) the graph plotted with that R code, with the caption above the graph.
My preference tends to be to use LaTeX packages to achieve customisation like this: there is a large community on Tex StackExchange who have developed methods to load of problems similar to this.
The floatrow package can be used to reposition the caption above the figure. This is largely based on this previous answer.
Using R Markdown, as this is the most typically used workflow these days, the package can be loaded by including header-includes argument within the YAML, as follows:
---
output: pdf_document
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
---
```{r fig.cap="cap, cap, and cap"}
plot(1)
```
The output has the desired order with the code displayed first, followed by the caption then the plot.
If the code is not wanted, the echo=FALSE option can be added to the chunk header.
Try using hook:
<<include=FALSE>>=
f <- function(x, options) {
paste("\\end{kframe}\n",
"\\caption{", options$capT, "}\n",
hook_plot_tex(x, options),
"\n\\begin{kframe}", sep = "")
}
knit_hooks$set(plot = f)
#
\begin{figure}
<<a-plot, echo=TRUE, capT="cap, cap, and cap">>=
plot(1)
#
\end{figure}
This is a slighty modified version of kohske's answer, that includes \begin{figure} and adds \label. Note however that it contains 5 lines, while the original code contains more than 150 lines, so it should be used in very limited settings.
f <- function(x, options) {
lab <- paste0(options$fig.lp, options$label)
paste("\\end{kframe}\n",
"\\begin{figure}\n\\caption{", options$capT, "}\\label{", lab,"}\n",
hook_plot_tex(x, options),
"\\end{figure}\n\n\\begin{kframe}", sep = "")
}
knit_hooks$set(plot = f)
Add a new block below it, with the same name, to print the code.
\documentclass{article}
\begin{document}
\begin{figure}
\caption{This is a caption above the figure}
<<a-plot, echo=FALSE>>=
plot(1)
#
\end{figure}
<<a-plot,echo=TRUE>>=
#
\end{document}
Why does the following command not produce a horizontal rule filling the space until the end of the line?
Hello \rule[0.5em]{\fill}{1pt}
It is my understanding that this should print the text “Hello” followed by a horizontal rule that extends until the end of the line, analogously to the macro \hfill which is effectively equivalent to \hspace\fill. – But in effect, this command just produces the text “Hello”, no rule.
I am aware that the effect can be produced by \hrulefill but it can’t be used here because I want a raised rule and \hrulefill doesn’t work together with \raisebox and I want my rule to hang above the baseline (at best in the middle of the line).
I don't have a satisfying answer as to why the command you presented doesn't work, but I can offer an effective workaround. Put
% Raised Rule Command:
% Arg 1 (Optional) - How high to raise the rule
% Arg 2 - Thickness of the rule
\newcommand{\raisedrule}[2][0em]{\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill}
into your document's preface, and then you can accomplish what you were hoping to with:
Hello \raisedrule[0.5em]{1pt}
The horizontal rule of 1pt height and raised by 1.5pt.
Hello \leaders\vrule height 2.5pt depth -1.5pt \hfill \null
There is a package called ulem which does this
% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass{article}
\usepackage[normalem]{ulem}
\begin{document}
normal text \uline{\textit{underline text}\hfill}
\end{document}
which will produce
For your curiosity, the option normalem for package ulem prevents ulem to produce extra underline with \em or \emph.
You can do this with the command \hrulefill
see http://en.wikibooks.org/wiki/LaTeX/Lengths#Fill_the_rest_of_the_line
% I did it!
%
\documentclass{article}
\usepackage[normalem]{ulem}
\begin{document}
\uline{Some text \hfill\phantom{.}}
\end{document}