Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm writing a document in latex and I can't get the cedilla character working within an equation block. My current charset is utf8, \c{c} don't work as stated in many LaTeX documents.
I'm trying the following:
\begin{equation}
\label{eq:accuracy}
confiança = \frac{tp + tn}{tp + fp + fn + tn}
\end{equation}
The workaround given here don't work.
There are two issues here. First, \c{c} does not work in math mode. You need to switch back to text mode (\textit preserves the italics and does not require amsmath):
\begin{equation}
\label{eq:accuracy}
confian\textit{\c{c}}a = \frac{tp + tn}{tp + fp + fn + tn}
\end{equation}
The second is that writing a full word in math mode will give you terrible kerning. Compare with this:
\begin{equation}
\label{eq:accuracy}
\textit{confian\c{c}a} = \frac{tp + tn}{tp + fp + fn + tn}
\end{equation}
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}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I want to write this simple equation in latex
2/|w|
In which |w| is the norm of w,,
I tried with
\frac{2}{\|w\|}
And get error "Missing $ inserted"
then I use this
\frac{2}{$\|w\|$}
And still get same error..
Does someone know how to solve this problem??
You are getting that error because LaTeX recognized this as being part of an equation and tried to correct it automagically. Unfortunately that rarely works well. You need to tell LaTeX that this is an equation.
To round out #nicoguaro's answer, use this form for equations that will be numbered and stand alone in the text:
\begin{equation}
\frac{2}{\left| w \right|}
\end{equation}
Or, use this form for equations that form part of a sentence:
blah blah $\frac{2}{\left| w \right|}$ blah blah.
Are this expression inside a mathematical environment like
\begin{equation}
...
\end{equation}
or $ $, \( \), \[ \] ?
The expression can be written as (using \[ \])
\[\frac{2}{\vert w \vert}\]
or just
\[\frac{2}{| w |}\]
You can see this book in Wikibooks. You need to specify that the whole expression is a mathematical expression enclosing it in its delimiters.
This question already has answers here:
How to have no pagebreak after \include in LaTeX
(5 answers)
Closed 8 years ago.
I am in the progress of writing an expose for my master thesis. One point in the expose is an overview of literature. To save my time and work I use bibtex to create that chapter. The thing I want to change now is that latex starts a new page for the bibliography which is an enormous overhead in a four page document.
The, I think, relevant parts from my document are:
\documentclass [ fontsize = 12pt,
paper = a4,
paper = portrait,
twoside = false,
headsepline,
twocolumn = false,
numbers=noenddot
]{scrartcl}
\bibliographystyle{unsrt}
\begin{document}
%
\include{text}
\nocite{*}
\singlespacing
\bibliography{literature}
%
\end{document}
I am using biblatex. This solved the problem:
\begingroup
\let\clearpage\relax
\printbibliography
\endgroup
How should a latex source code listing look like to produce an output like in known books, for example one for the Spring Framework? I've tried with the latex listings package but wasn't able to produce something that looked as nice as the one below. So I'm primarely interested in the formatting instructions to produce something like the sample below (from Manning's sample chapter for Spring in Action):
EDIT
With the help especially of Tormod Fjeldskår here's the complete snippet to produce the desired look:
\usepackage{listings}
\usepackage{courier}
\lstset{
basicstyle=\footnotesize\ttfamily, % Default font
% numbers=left, % Location of line numbers
numberstyle=\tiny, % Style of line numbers
% stepnumber=2, % Margin between line numbers
numbersep=5pt, % Margin between line numbers and text
tabsize=2, % Size of tabs
extendedchars=true,
breaklines=true, % Lines will be wrapped
keywordstyle=\color{red},
frame=b,
% keywordstyle=[1]\textbf,
% keywordstyle=[2]\textbf,
% keywordstyle=[3]\textbf,
% keywordstyle=[4]\textbf, \sqrt{\sqrt{}}
stringstyle=\color{white}\ttfamily, % Color of strings
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
% backgroundcolor=\color{lightgray},
showstringspaces=false
}
\lstloadlanguages{ % Check documentation for further languages ...
% [Visual]Basic,
% Pascal,
% C,
% C++,
% XML,
% HTML,
Java
}
% \DeclareCaptionFont{blue}{\color{blue}}
% \captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
Use it with this in your document:
\lstinputlisting[label=samplecode, caption=A sample]{sourceCode/HelloWorld.java}
It seems to me that what you really want, is to customize the look of the captions. This is most easily done using the caption package. For instructions how to use this package, see the manual (PDF). You would probably need to create your own custom caption format, as described in chapter 4 in the manual.
Edit: Tested with MikTex:
\documentclass{report}
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
% This concludes the preamble
\begin{document}
\begin{lstlisting}[label=some-code,caption=Some Code]
public void here() {
goes().the().code()
}
\end{lstlisting}
\end{document}
Result:
I am happy with the listings package:
Here is how I configure it:
\lstset{
language=C,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
}
I use it like this:
\begin{lstlisting}[caption=Caption example.,
label=a_label,
float=t]
// Insert the code here
\end{lstlisting}
And please, whatever you do, configure the listings package to use fixed-width font (as in your example; you'll find the option in the documentation). Default setting uses proportional font typeset on a grid, which is, IMHO, incredibly ugly and unreadable, as can be seen from the other answers with pictures. I am personally very irritated when I must read some code typeset in a proportional font.
Try setting fixed-width font with this:
\lstset{basicstyle=\ttfamily}
I wonder why nobody mentioned the Minted package. It has far better syntax highlighting than the LaTeX listing package. It uses Pygments.
$ pip install Pygments
Example in LaTeX:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{minted}
\begin{document}
\begin{minted}{python}
import numpy as np
def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int) #dummy variable
#compute the bitwise xor matrix
M1 = bitxormatrix(genl1)
M2 = np.triu(bitxormatrix(genl2),1)
for i in range(m-1):
for j in range(i+1, m):
[r,c] = np.where(M2 == M1[i,j])
for k in range(len(r)):
VT[(i)*n + r[k]] = 1;
VT[(i)*n + c[k]] = 1;
VT[(j)*n + r[k]] = 1;
VT[(j)*n + c[k]] = 1;
if M is None:
M = np.copy(VT)
else:
M = np.concatenate((M, VT), 1)
VT = np.zeros((n*m,1), int)
return M
\end{minted}
\end{document}
Which results in:
You need to use the flag -shell-escape with the pdflatex command.
For more information: https://www.sharelatex.com/learn/Code_Highlighting_with_minted
Have a try on the listings package. Here is an example of what I used some time ago to have a coloured Java listing:
\usepackage{listings}
[...]
\lstset{language=Java,captionpos=b,tabsize=3,frame=lines,keywordstyle=\color{blue},commentstyle=\color{darkgreen},stringstyle=\color{red},numbers=left,numberstyle=\tiny,numbersep=5pt,breaklines=true,showstringspaces=false,basicstyle=\footnotesize,emph={label}}
[...]
\begin{lstlisting}
public void here() {
goes().the().code()
}
[...]
\end{lstlisting}
You may want to customize that. There are several references of the listings package. Just google them.
Take a look at algorithms package, especially the algorithm environment.
There are several other things you can do, such as selecting new fonts:
\documentclass[10pt,a4paper]{article}
% ... lots of packages e.g. babel, microtype, fontenc, inputenc &c.
\usepackage{color} % Leave this out if you care about B/W printing, obviously.
\usepackage{upquote} % Turns curly quotes in verbatim text into straight quotes.
% People who have to copy/paste code from the PDF output
% will love you for this. Or perhaps more accurately:
% They will not hate you/hate you less.
\usepackage{beramono} % Or some other package that provides a fixed width font. q.v.
% http://www.tug.dk/FontCatalogue/typewriterfonts.html
\usepackage{listings}
\lstset { % A rudimentary config that shows off some features.
language=Java,
basicstyle=\ttfamily, % Without beramono, we'd get cmtt, the teletype font.
commentstyle=\textit, % cmtt doesn't do italics. It might do slanted text though.
\keywordstyle= % Nor does cmtt do bold text.
\color{blue}\bfseries,
\tabsize=4 % Or whatever you use in your editor, I suppose.
}
\begin{document}
\begin{lstlisting}
public final int ourAnswer() { return 42; /* Our final answer */ }
\end{lstlisting}
\end{document}
For R code I use
\usepackage{listings}
\lstset{
language=R,
basicstyle=\scriptsize\ttfamily,
commentstyle=\ttfamily\color{gray},
numbers=left,
numberstyle=\ttfamily\color{gray}\footnotesize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
title=\lstname,
escapeinside={},
keywordstyle={},
morekeywords={}
}
And it looks exactly like this