How to draw a figure with a discrete colormap in rmarkdown - latex

I would like to draw a figure with a discrete colormap using rmarkdown.
I tried using pgfplots. The following code should give my a figure with a discrete colormap instead of a continous one. Using the same code in Latex works as expected.
In the attached log file, I didn't find any clues as to what is going wrong. But I am also not sure what to look for, since there are no errors. Note that I included the undefined control sequence at the end on purpose to cause an error. Otherwise the log file is deleted right after compilation.
figure with continous colorbar
---
output:
pdf_document: default
html_document:
df_print: paged
header-includes:
- \usepackage{pgfplots}
- \pgfplotsset{compat=1.13}
---
\begin{tikzpicture}
\begin{axis}[
domain=-2:2,
view={0}{90},
colormap={CM}{
samples of colormap=(13 of hot)},
colormap access=piecewise constant,
colorbar right,
colorbar style={%
ytick=data,
}
]
\addplot3[surf,shader=interp]
{exp(-x^2-y^2)*x+0.5};
\end{axis}
\end{tikzpicture}

Related

How can i get a symbol, produced by putting one character on the surface on the other, in LaTeX?

I need to type in my document, but I can't find the proper way to do it. It looks like it was made by combining $\Diamond$ and $\Box$ somehow. So how can i get it?
If you can't find the symbol available somewhere, I would think using tikz would be an easy solution. Small tikz pictures can be inlined or defined as a command. The symbol can be created as contents of overlapping nodes, the node shapes or a handmade tikz path. The following code shows some examples:
\documentclass{article}%
\usepackage{tikz}%
\usepackage{amssymb}
\usetikzlibrary{shapes,math}
\newcommand{\myawesomesymbol}[1]{\tikz[baseline=-1mm]{\node[diamond, draw, inner sep=#1]{}; \node[draw, inner sep=#1]{};}}
\newcommand{\mythicksymbol}[1]{\tikz[baseline=-1mm]{\node[thick, diamond, draw, inner sep=#1]{}; \node[thick, draw, inner sep=#1]{};}}
\newcommand{\myhandmadesymbol}[1]{\tikz[baseline=-1mm]{
\draw[] (-#1,-#1) -- (#1,-#1) -- (#1,#1) -- (-#1,#1) -- (-#1,-#1);
\draw[] (-2.1*#1, 0) -- (0,2.1*#1) -- (2.1*#1,0) -- (0, -2.1*#1)-- (-2.1*#1,0);}}
\begin{document}
This is some sentence with this symbol \tikz[baseline=-1mm]{\node[diamond, draw]{}; \node[draw]{};} in the text.
This one \myawesomesymbol{.7mm} is smaller and as a command and there is a larger one: \myawesomesymbol{3mm}, or scaling with the font size: {\tiny tiny \myawesomesymbol{.5ex}}, {\small small \myawesomesymbol{.5ex}}, Large {\Large \myawesomesymbol{.5ex}}.
Maybe thick: \mythicksymbol{1.5mm}.
Using tikz, it is also possible to overlay math symbols as suggested in the question: \tikz[baseline=-1mm]{\node[]{\Huge$\diamond$}; \node[]{\tiny$\Box$};}, however, the spacing is not perfect.
And last, in case the little overlapping corners are a concern, it is still possible drawing it by hand: small \myhandmadesymbol{.1}, larger \myhandmadesymbol{.2}, scaling with font \myhandmadesymbol{.5ex}.
\end{document}
Output:

Create new variable each time an environment in LaTeX is called

I've created an example environment for my maths notes. It takes the title of the example as the input and draws some lines with tikz. However, to do so, it requires the length of the title.
This is relatively easy to do when the environment is only called once by using \newlength{\lengthname} followed by \settowidth{\lengthname}{[length]}. However, as soon as it is called more than once, a different length must be defined. My (admittedly poor) work-around has been to pass the name of a different length, #2, every time I use my example environment.
How can I create a unique \newlength{\unique} each time I use my environment, or, is there some better way of achieving my goal?
\newenvironment{example}[2] % Example Environment
{\refstepcounter{example}
\newlength{#2}
\settowidth{#2}{\small \textbf{Example \thesection.\theexample} --- #1}
\bigskip\begin{tikzpicture}
\draw (-0.5\columnwidth,-0.2)--(-0.5\columnwidth,0)--(0.5\columnwidth,0)--(0.5\columnwidth,-0.2);
\fill[white] (-0.5#2-5pt,-1pt) rectangle (0.5#2+5pt,1pt);
\tikzlabel{0}{-0.4}{\text{\small \textbf{Example \thesection.\theexample} --- #1}}
\end{tikzpicture}}
%
{\begin{tikzpicture}
\draw (-0.5\columnwidth,0.2) -- (-0.5\columnwidth,0) -- (0.5\columnwidth,0) -- (0.5\columnwidth,0.2);
\end{tikzpicture}}
Many thanks.
My suggestion would be to use tcolorbox instead of drawing the frame yourself, but if you must use tikz, simply use a white background for your title.
Please note that your code would produce a lot of overfull box warnings. You have to consider the indention and drawing a frame of column won't fit because you need an additional two times the half the width of the tikz lines. I simply reduced the width to .49\columnwidth, but you could also take into account the width of the line in your calculation.
Also pay attention to the spacing around the ---. If you don't prevent the macro before from swallowing the space, it won't be centred.
\documentclass{article}
\usepackage{tikz}
\newcounter{example}
\newenvironment{example}[1]{%
\refstepcounter{example}%
\bigskip
\noindent%
\begin{tikzpicture}
\draw (-0.49\columnwidth,-0.2)--(-0.49\columnwidth,0)--(0.49\columnwidth,0)--(0.49\columnwidth,-0.2);
\node[fill=white,font=\small\bfseries] at (0,-1pt) {Example \thesection.\theexample{} --- #1};
\end{tikzpicture}%
\par%
}{%
\par%
\noindent%
\begin{tikzpicture}
\draw (-0.49\columnwidth,0.2) -- (-0.49\columnwidth,0) -- (0.49\columnwidth,0) -- (0.49\columnwidth,0.2);
\end{tikzpicture}%
\par%
}
\begin{document}
\begin{example}{test}
content...
\end{example}
\end{document}

How to center LaTeX xtable (And Figure) output in full text width

This is a follow-up to a question I posted earlier (How to center LaTeX xtable output in full text width).
I realize that my MWE from this previous post was incomplete. In an effort to make it as minimal of an example as possible, I did leave out something that ended up conflicting. Hence, here, I am posting the issue more fully.
I am using tufte-handout (http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf) to create a small report in latex. I have a file code.Rnw that I knit into code.tex. Below is my code.Rnw:
\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[space]{grffile}
\usepackage{geometry}
\usepackage{pgffor}
\usepackage{calc}
\usepackage{enumitem}
\usepackage{microtype}
\usepackage{tabularx}
%\usepackage{floatrow}
\begin{document}
<<include=FALSE>>=
library(ggplot2)
library(xtable)
#
\begin{fullwidth}
\makeatletter\setlength\hsize{\#tufte#fullwidth}\makeatother
<<echo=FALSE,results='asis'>>=
x.big <- xtable(mtcars[1:10,1:4], label ='tab:mtcars',caption ='This is the mtcar dataset head.',align = c("rr|lr|r"))
print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
#
\end{fullwidth}
<<echo=FALSE,results='asis'>>=
fnameO <<- "plot.pdf"
pdf(paste0("./",fnameO),width=6,height=7)
print(qplot(hp, mpg, data=mtcars, main="Scatterplots of MPG vs. Horsepower", xlab="Horsepower", ylab="Miles per Gallon"))
{dev.off();invisible()}
#
\begin{fullwidth}
\makeatletter\setlength\hsize{\#tufte#fullwidth}\makeatother
\begin{figure}[!ht]
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
\end{document}
This is the output:
I am desiring to have both the table and the figure centered (across the whole page). As shown above, I am successfully able to get the table centered (thanks to advice from a user in my previous post).
However, I am unable to get the figure centered across the whole page with the caption below it. Instead, likely due to the document class I am using (tufte-handout), the figure itself is in the non-margin area, and its caption is in the margin area.
For starters, I uncommented the \usepackage{floatrow} in the code, in an attempt to force the figure caption to be below the figure instead of to the right of it. This lead to an output as such (where both the table and figure are undesirably on the left side instead of centered, but the figure caption is indeed below the figure):
My question is: How can I center both the table and figure (with the caption below it), so that the output would look more like this?:
Thank you.
As a crude hack, you could simply modify the figure environment of your MWE to use the original \caption command:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\#tufte#fullwidth}\setlength\linewidth{\#tufte#fullwidth}\let\caption\#tufte#orig#caption\makeatother
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
...or, for a bit smaller figure that is centered on the page:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\#tufte#fullwidth}\setlength\linewidth{\#tufte#fullwidth}\let\caption\#tufte#orig#caption\makeatother
\centering
\includegraphics[width=.4\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
If you find the original \caption command lacking and if none(!) of the floats in your document need to use tufte captions you can overwrite the \caption command using something like \usepackage[labelfont=bf,compatibility=false]{caption}.

LaTeX: Large figure floats to the end of the chapter

I have a LaTeX-document using the class
\documentclass[12pt,a4paper]{scrbook}
and I changed some parameters for positioning the floats:
\renewcommand{\topfraction}{1} %default: 0.7
\renewcommand{\bottomfraction}{1} %default: 0.3
\renewcommand{\textfraction}{0.1} %default: 0.2
\renewcommand{\floatpagefraction}{1} %default: 0.6
\setcounter{topnumber}{3}
\setcounter{bottomnumber}{3}
I have two graphics that should be among each other and fill one single page. I don't know why, but because LaTeX always splits both graphics on two pages, I put both graphics into one figure-environment. It doesn't matter whether I use [ht] or [p] the figure with both graphics moves to the end of the chapter. I don't get any overfull warnings. So I think it might not the reason that the figure is too large.
\begin{figure}[p]
\centering
\includegraphics{graphic1.pdf}
\newcaption{caption 1} % <-- using \usepackage{picins}
\label{fig:pic1}
\vspace{5mm}
\includegraphics{graphic2.pdf}
\newcaption{caption 2}
\label{fig:pic2}
\end{figure}
Is there someone, who can explain that behaviour of LaTeX and can recommend a solution.
This is standard behaviour in latex because it takes care of all positioning. I personally wouldn't mess this up and would instead refer to the images.
If you nevertheless want to position them at the given line in the text:
Use the package float and place your images this way.
\usepackage{float}
\begin{figure}[H]
...
\end{figure}
I misunderstood the meaning of \floatpagefraction....
It describes the minimum size that a float must have, so that you can use [p]. Setting it to a value of 1 (100% of the page) makes definitely no sense.
bad:
\renewcommand{\floatpagefraction}{1}
good:
\renewcommand{\floatpagefraction}{0.6} %default: 0.6
Default values generally are not so bad......
If you want to have your images right where you want them to be, just use
/usepackage{float}
and edit your image code as follows.
\begin{figure}[H]
\centering
\includegraphics{graphic1.pdf}
\newcaption{caption 1} % <-- using \usepackage{picins}
\label{fig:pic1}
\end{figure}
\vspace{5mm}
\begin{figure}
\includegraphics{graphic2.pdf}
\newcaption{caption 2}
\label{fig:pic2}
\end{figure}

How do I correctly insert an image/figure and have the text "flow around" the image/figure in latex?

I've been struggling with this. I wanted to insert an image and have it 'near' the text that discusses it, but have the text on that page wrap/flow around the image.
The image I've converted into eps format. I initially tried to use the figure environment (\begin{figure}...), but that merely placed the image at the top or bottom of the page without any text beside it, leaving a large portion of the page empty.
I did some digging on the web and identified the 'wrapfig' package, it seemed a likely solution, but I get a series of errors, and the image appears at the end of the document.
The errors:
Package wrapfig Warning: wrapfigure used inside a conflicting environment on input line 297.
Package wrapfig Warning: Stationary wrapfigure forced to float on input line 303.
Package wrapfig Warning: Stationary wrapfigure forced to float on input line 306.
Which continues for several lines.
What's odd is that one occasion, after compiling, the image appeared exactly where I wanted it, and then on the next it didn't.
[Added a minute or so later]
The latex code I have currently:
\begin{wrapfigure}{r}{0.2\textwidth}[h]
\begin{center}
\includegraphics[width=0.18\textwidth]{vec-perp.eps}
\end{center}
\caption{A}
\end{wrapfigure}
wrapfigure does not need the [h] specifier.
you need to include the wrapfigure package in your preamble:
\usepackage{wrapfig}
then, put the wrapfigure call above the text you want to wrap into, like this:
\begin{wrapfigure}{r or l}{width/height} \centering \includegraphics[width/height]{graphic.filename} \caption{foo} \end{wrapfigure}
a real world example:
\begin{wrapfigure}{r}{1.5in}
\centering
\includegraphics[width=1.5in]{smile.jpg}
\end{wrapfigure}
I just went through my document, commenting it out in sections, hoping to find the environment it was complaining about...in the process, I unintentionally introduced a blank line that I didn't have before. Apparently, the environment it was complaining about was the environment before the figure. I didn't have a blank line between the previous part, which was an itemize environment.
So...this, for example, is 'broken':
Ingredients for the Banana-Grape Bread Recipe
\begin{itemize}
\item Bananas
\item Grapes
\item Eggs
\end{itemize}
\begin{wrapfigure}{r}{0.2\textwidth}
\centering
\includegraphics[width=0.18\textwidth]{banana-grape.eps}
\caption{BananaGrape Bread}
\end{wrapfigure}
And inserting a empty line:
\end{itemize}
\begin{wrapfigure}{r}{0.2\textwidth}
Clears up my problems. Along the way I learned all sorts of things, yay! On the other hand, I'm pretty sure I don't have a clear understanding of environments yet. Time to spend some time reading, I reckon.

Resources