How to place a character below a function in Latex? - math

Is it possible to place a character or a formula below an other part of a larger formula in Latex?
foo f(x) = ...
x
In case that this example is not clear. I'd like to make one of my custom functions - just defined as \text{foo} in a math environment - look like one of the built-in functions like sum, min or max which accept parameters that are placed above or below the function symbol.
Simply using \text{foo}_x does not work.

The function you're looking for is \underset provided by the amsmath package. Here's an example:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\underset{below}{above}$
\end{document}
Output:

You can declare the foo as a math operator. Then use it in any equation environment. See below
\DeclareMathOperator*{\foo}{foo} %in preamble
\begin{align}
\foo_x f(x) = ...
\end{align}

Following the line of the answer by #hanoosh, if you need you can even do:
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator*{\foo}{foo}
\begin{document}
\begin{displaymath}
\foo_{x\atop y} f(x,y)
\end{displaymath}
\end{document}
Only \DeclareMathOperator* is powered by package amsmath while, for example,
\documentclass{article}
\begin{document}
\begin{displaymath}
\sum_{x\atop y} f(x,y)
\end{displaymath}
\end{document}
compiles not needing any AMS package.

Related

Newtonian notation for derivates on latex

I am using Notion for my studies: so I have to use Latex to represent the equations or others math stuffs. I noticed that I can use the Newtonian notation on Latex with the command \dot or \ddot (first derivative and second derivative) but I cannot represent the third with \dddot or with something else. Is there a way to represent that?
As in this other answer, three dots (\dddot) and four dots (\ddddot) in math mode require the package amsmath.
So you have either
\documentclass{article}
\begin{document}
\[\dot{x}\]
\[\ddot{x}\]
\end{document}
or
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[\dot{x}\]
\[\ddot{x}\]
\[\dddot{x}\]
\[\ddddot{x}\]
\end{document}

How do I write this complicated formula in R Markdown?

I have been trying for days to figure out how to write this formula in R markdown...
...but I didn't find anything on the web.
Can you help me please?
Thanks.
You can use something like that
\documentclass{article}
\begin{document}
\[
x_1 = \left(\begin{array}{c}x_{11}\\\vdots\\x_{1p}\end{array}\right),
\cdots,
x_n = \left(\begin{array}{c}x_{n1}\\\vdots\\x_{np}\end{array}\right)
\]
\end{document}
The array is used to stack elements and the \left( \right) pair provides the enclosing parenthesis.
Does not require any package and can probably be pasted as is in R.

Avoid code-chunks from breaking in Knitr? ( preferably using a chunk option )

Using knitr to create a pdf, codechunks break according to page breaks. Usually this is exactly what I want, but in some cases I would like to be able to avoid this. E.g. by making a code-chunk jump to the next page if it does not fit the current page. I would prefer if this could be done in a chunk option, I.E not using eg. \newpage etc.
The following is an example of a code-chunk that breaks. How do I avoid this?
\documentclass{article}
\usepackage[english]{babel}
\usepackage{lipsum}
\begin{document}
\lipsum[1-3] \textbf{The following chunk will break. How do I avoid this breaking? }
<<echo=TRUE>>=
(iris)[1:20,]
#
\end{document}
I left an empty environment knitrout in the knitr design for such purposes. You can redefine this environment to achieve what you want. There are many LaTeX environments that are non-breakable, such as a figure environment. Below I use the minipage environment as an example:
\documentclass{article}
\renewenvironment{knitrout}{\begin{minipage}{\columnwidth}}{\end{minipage}}
% alternatively, you can use `figure`
% \renewenvironment{knitrout}{\begin{figure}}{\end{figure}}
\begin{document}
\begin{figure}
\caption{One figure.}
\end{figure}
% placeholder
\framebox{\begin{minipage}[t][0.3\paperheight]{1\columnwidth}%
nothing
\end{minipage}}
<<echo=TRUE>>=
(iris)[1:20,]
#
\begin{figure}
\caption{Another one.}
\end{figure}
\end{document}

figure* environment in twocolumn knitr/Sweave document

Sounds like it should be a common problem, but I didn't find an obvious trick.
Consider the knitr Rnw file below,
\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf, fig.align=center}
\begin{figure*}
<<aaa, fig.width=8, fig.height=5, fig.show=hold>>=
plot(1,1)
#
\end{figure*}
\end{document}
I would like this wide figure to span two columns, using a {figure*} LaTeX environment. Is there a hook for that?
EDIT: wrapping the chunk in figure* gives the following output.
Two facts:
knitr makes everything accessible for you, so LaTeX tricks are often unnecessary;
there is a chunk hook with which you can wrap your chunk results;
A simple-minded solutions is:
knit_hooks$set(chunk = function(x, options) {
sprintf('\\begin{figure*}\n%s\n\\end{figure*}', x)
})
I leave the rest of work to you to take care of more details in options (e.g. when options$fig.keep == 'none', you should not wrap the output in figure*). You may want to see how the default chunk hook for LaTeX is defined in knitr to know better how the chunk hook works.
However, in this case, I tend to write the LaTeX code by myself in the document instead of automatically creating it. After you have got figure*, you may start to think about \caption{} and \label{} (not hard, but I still want to see them in LaTeX).
Not sure about how knitr but for Sweave (and basic latex) there is in fact a trick: have the R code produce a pdf file, and then use standard \includegraphics to pull it in.
So with this:
\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf}
<<aaa,fig=FALSE,print=FALSE,echo=FALSE>>=
pdf("mychart.pdf", width=6, height=3)
set.seed(42)
plot(cumsum(rnorm(100)), type='l', main="yet another random walk")
invisible(dev.off())
#
\begin{figure*}
\includegraphics{mychart.pdf}
\end{figure*}
\end{document}
I got the document below (which I then converted from pdf to png):
I also had a similar problem while preparing a figure that should span two columns in a IEEE two-column conference paper.
Setting the chunk hook caused some strange error in my setup.
Even this simple hook: knit_hooks$set(chunk = function(x, options) x)
But after looking into knitr::opts_chunk$get(), I realized that simply setting fig.env="figure*" solves the problem in an elegant way.
Here is how my chunk looks like in an Rnw file:
<<fig1, fig.width=18, fig.height=6, fig.env="figure*">>=
#

R / Sweave: Prefixing and figure file naming

I am new to R and much newer to Sweave and I am experimenting with graphics. Here is a sample code:
\documentclass[a4paper]{article}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=true}
\begin{document}
<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
#
\SweaveOpts{prefix.string=Evolution}
<<label=amount,echo=FALSE,results=hide>>=
postscript('doudou.eps',
width=6, height=7,
colormodel="cmyk",
family = "ComputerModern")
with(test.frame,plot(year, value))
dev.off()
#
\begin{figure}[htbp]
\begin{center}
\includegraphics[width=1\textwidth,angle=90]{doudou.eps}
\end{center}
\end{figure}
\end{document}
What I want to do above?
To have manual control over the EPS file that I am inserting so that I can have the \includegraphicscommand in the Sweave file itself.
And I am trying to give a proper file name to the figure: with prefix Evolution and label amount such that the EPS figure produced will be named Evolution-amount.eps.
What is going wrong?
As you can see, I am inserting a file name in the R postscript option i.e. doudou.eps . If I don't do this a file named Rplots.ps is created by R.
So my code is well ignoring the prefix and label that I want to give to my figure file.
And I am explicitly asking later on by \includegraphics to put doudou.eps.
How I want it to be?
To be able to have prefix and label as I mentioned above in the figure file name, while I still have manual control over the \includegraphics command in the Sweave file. Is this possible?
What is the use of this?
Say I am writing a paper and I have figures in different sections. So it would be nice to have something like:
\SweaveOpts{prefix.string=Paper2}
<<label=section2,echo=FALSE,results=hide>>=
and for example I specify in the postscript option: model.eps.
Then the figure would be named Paper2-section2.model.eps for example. Is that feasible?
And I would need to put this name somehow manually ?? in the \includegraphics command that follows.
Thanks a lot...
Update: 09 dec 2011.
A close solution with the help of cbeleites is:
\documentclass[fleqn, a4paper,12pt]{article}
\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=TRUE}
\begin{document}
<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
#
\SweaveOpts{prefix.string=Paper2}
<<label=section2, echo=FALSE,results=hide>>=
ps.options ( width=6, height=7, colormodel="cmyk", family = "ComputerModern")
#
<<label=section2, fig=TRUE, include = TRUE, echo=FALSE>>=
with(test.frame,plot(year, value))
#
\end{document}
On compilation I get the EPS and PDF files named Paper2-section2. This is as close as we can get I think.
You need to use a figure chunk.
<<fig=TRUE, include = FALSE>>=
with(test.frame,plot(year, value))
#
You can pass more options to the figure chunk, such as width and height.
for more advanced options, use R's postscript options:
<<>>=
ps.options (colormodel="cmyk", family = "ComputerModern")
#
if the options apply only to one figure, you'd probably be easier off without figure chunk and with manual \includegraphics. You could give the filename to the ps file as if it was produced by a Sweave figure chunk, though.
If the only reason for \includegraphics is that you want to control its options, have a look at graphicx' Gin:
\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=\linewidth}
<<fig=TRUE>>=
with(test.frame,plot(year, value))
#
\end{center}
\end{figure}
I thought Gin would work with the rotation as well, but it doesn't. see https://tex.stackexchange.com/a/31013 for explanation and possible solution. However, if the rotation is only because the figures appear sideways (as opposed to you wanting them to be sideways) this may be solved with correct ps settings in R (RSiteSearch () and ? postscript should help).

Resources