Latex two captioned verbatim environments side-by-side - layout

How to get two verbatim environments inside floats with automatic captioning side-by-side?
\usepackage{float,fancyvrb}
...
\DefineVerbatimEnvironment{filecontents}{Verbatim}%
{fontsize=\small,
fontfamily=tt,
gobble=4,
frame=single,
framesep=5mm,
baselinestretch=0.8,
labelposition=topline,
samepage=true}
\newfloat{fileformat}{thp}{lof}[chapter]
\floatname{fileformat}{File Format}
\begin{fileformat}
\begin{filecontents}
A B C
\end{filecontents}
\caption{example.abc}
\end{fileformat}
\begin{fileformat}
\begin{filecontents}
C B A
\end{filecontents}
\caption{example.cba}
\end{fileformat}
So basically I just need those examples to be side-by-side (and keeping automatic nunbering of caption). I've been trying for a while now.

For captioning verbatim environments you can either use listings (which will offer much more than just plain captioning, syntax highlighting and line numbering come for free too) or define your own float environment using the package with the same name.
An example (from WikiBooks):
\documentclass{article}
\usepackage{float}
\floatstyle{ruled}
\newfloat{program}{thp}{lop}
\floatname{program}{Program}
\begin{document}
\begin{program}
\begin{verbatim}
class HelloWorldApp {
public static void main(String[] args) {
//Display the string
System.out.println("Hello World!");
}
}
\end{verbatim}
\caption{The Hello World! program in Java.}
\end{program}
\end{document}

Found the soulution finally.
\usepackage{caption}
\begin{fileformat}[h]
\centering
\begin{minipage}[b]{0.4\textwidth}
\begin{filecontents}
A B C
\end{filecontents}
\captionof{fileformat}{example.abc}
\end{minipage}
\quad
\begin{minipage}[b]{0.4\textwidth}
\begin{filecontents}
C B A
\end{filecontents}
\captionof{fileformat}{example.cba}
\end{minipage}
\end{fileformat}
The problem solution is to make a caption independently from environment using caption package macro \captionof{fileformat}{Our Caption}.

Use minipage like in this example, which places two pictures side by within a figure float with separate captions
\begin{figure}[htbp]
\centering
\begin{minipage}[b]{5 cm}
\includegraphics{filename 1}
\caption{caption 1}
\label{labelname 1}
\end{minipage}
\begin{minipage}[b]{5 cm}
\includegraphics{filename 2}
\caption{caption 2}
\label{labelname 2}
\end{minipage}
\end{figure}

Related

Latex nested environment: align, linenomath, split

I'm writing a document with a lot of equations and I would like to create new environments to write my equations in. Only for convenience, it works without, but it is fastidious to type.
For single equations, I want to number the lines with lineo. With align environment, only use 1 equation number with the split environment, and number the lines. So I can do this in my document:
\usepackage[displaymath, mathlines]{lineno}
\linenumbers
...
\begin{linenomath}
\begin{equation}
\end{equation}
\end{linenomath}
and
\begin{linenomath}
\begin{align
\begin{split}
\end{split}
\end{align}
\end{linenomath}
This works, however, I'm trying to create new environments for it like this:
\newenvironment{my_equation}
{\begin{linenomath}
\begin{equation}
}
{
\end{equation}
\end{linenomath}
}
\newenvironment{my_align}
{\begin{linenomath}
\begin{align
\begin{split}
}
{
\end{split}
\end{align}
\end{linenomath}
}
These do not work. I got an error and the file won't compile. Am I doing things correctly? Which obvious solution did I miss?
Thanks for your help!
You can use the environ package:
\documentclass{article}
\usepackage{mathtools}
\usepackage[mathlines]{lineno}
\usepackage{environ}
\NewEnviron{my_equation}
{\begin{linenomath}
\begin{equation}
\BODY
\end{equation}
\end{linenomath}
}
\NewEnviron{my_align}
{\begin{linenomath}
\begin{align}
\begin{split}
\BODY
\end{split}
\end{align}
\end{linenomath}
}
\begin{document}
\begin{my_equation}
test
\end{my_equation}
\begin{my_align}
test
\end{my_align}
\end{document}

Formatting "key: value" strings with middle spaces

I'm showing key-value pair in a QListWidget. Since key names have different lengths, numbers start in different positions:
TestParameter1: 1.2345
Param2: 6.7890
If it wasn't for the key name, I know that I can use format to introduce spaces with a syntax like '{:7.4f}'.format(value).
Is there any easy way (I mean, not switching to a table or creating my own implementation os a QListView) of achieving something like...?:
TestParameter1: 1.2345
Param2: 6.7890
I suggest you take a further look at string formatting here.
The first thing that comes to mind is to either use the \t (tab) to align as necessary; or to use something on the lines of the below as stated in the python docs.
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
... '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>

Latex: How to encapsulate text in an amsmath block

My first stackoverflow question. I take the easy way out and directly ask the following. How is it possible to produce an output in LaTeX, like the one pictured below?
More holistic, what are the best ways to encapsulate text in an amsmath eg. equation, align, block?
You should use a tabular environment. This code:
\documentclass{article}
\begin{document}
\begin{tabular}{ll}
$ID_V$ & = identifier of $V$\\
$P_C$ & = password of user on $C$\\
$AD_C$ & = network address of $C$\\
$K_V$ & = secret encryption key shared by $AS$ and $V$
\end{tabular}
\end{document}
produces:
Then you can indent or align the tabular as you prefer.
Only a hint about the second part of your question. Keep in mind which environments, like tabular, have text as default content (hence math must be included in $...$) and which ones, like array, align, equation, etc., have math as default (so that text must be included in a box or in \text{...}).
You can use the \text{} command to write text in amsmath blocks:
\documentclass[11pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
ID_V &= \text{identifier of $V$} \\
P_C &= \text{password of user on $C$}
\end{align}
\begin{alignat}{2}
&ID_V &&= \text{identifier of $V$} \\
&P_C &&= \text{password of user on $C$}
\end{alignat}
\end{document}

How to use pgfplotstabletypeset in combination with sweave

I am trying to make a table in LateX in which the background colour of my cells depend on the size of a number. I found some answers on https://tex.stackexchange.com/questions/42444/parametrize-shading-in-table-through-tikz but I cannot get them working in combination with Sweave. Below is the first answer given on the aformentioned page, which works up until the part where I use Sweave, though I have the impression the Sweave gives the correct output.
I have also tried other answers on this topic, but I can get none of them working in combination with Sweave. If someone can get the code below working, or knows of another way to do this with Sweave, it would be greatly appreciated!
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotstableset{
color cells/.style={
col sep=comma,
string type,
postproc cell content/.code={%
\pgfkeysalso{#cell content=\rule{0cm}{2.4ex}\cellcolor{black!##1}\pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}%
},
columns/x/.style={
column name={},
postproc cell content/.code={}
}
}
}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,a1,b1
a,90,10.5,0,0
b,0,80,10,10
a1,0,0,95,5
b1,0,10,5,85
}
\end{table}
\begin{table}\caption{Correlation or something2}
\centering
\pgfplotstabletypeset[color cells]{
<<echo=FALSE, results=tex>>=
xx<-data.frame(a=c(90,10.5,0,0),b=c(0,80,10,10),a1=c(0,0,95,5),b1=c(0,10,5,85))
rownames(xx)=c('a','b','a1','b1')
for(i in 0: nrow(xx)){
if(i==0){
cat(c("x,",paste0(colnames(xx)[1:(ncol(xx)-1)],","),colnames(xx)[ncol(xx)]),"\n")
}else{
cat(paste0(rownames(xx)[i],","),paste0(xx[i,1:(nrow(xx)-1)],","),xx[i,nrow(xx)],"\n")
}
}
#
}
\end{table}
\end{document}

String replacement in latex

I'd like to know how to replace parts of a string in latex. Specifically I'm given a measurement (like 3pt, 10mm, etc) and I'd like to remove the units of that measurement (so 3pt-->3, 10mm-->10, etc).
The reason why I'd like a command to do this is in the following piece of code:
\newsavebox{\mybox}
\sbox{\mybox}{Hello World!}
\newlength{\myboxw}
\newlength{\myboxh}
\settowidth{\myboxw}{\usebox{\mybox}}
\settoheight{\myboxh}{\usebox{\mybox}}
\begin{picture}(\myboxw,\myboxh)
\end{picture}
Basically I create a savebox called mybox. I insert the words "Hello World" into mybox. I create a new length/width, called myboxw/h. I then get the width/height of mybox, and store this in myboxw/h. Then I set up a picture environment whose dimensions correspond to myboxw/h. The trouble is that myboxw is returning something of the form "132.56pt", while the input to the picture environment has to be dimensionless: "\begin{picture}{132.56, 132.56}".
So, I need a command which will strip the units of measurement from a string.
Thanks.
Use the following trick:
{
\catcode`p=12 \catcode`t=12
\gdef\removedim#1pt{#1}
}
Then write:
\edef\myboxwnopt{\expandafter\removedim\the\myboxw}
\edef\myboxhnopt{\expandafter\removedim\the\myboxh}
\begin{picture}(\myboxwnopt,\myboxhnopt)
\end{picture}
Consider the xstring package at https://www.ctan.org/pkg/xstring.
The LaTeX kernel - latex.ltx - already provides \strip#pt, which you can use to strip away any reference to a length. Additionally, there's no need to create a length for the width and/or height of a box; \wd<box> returns the width, while \ht<box> returns the height:
\documentclass{article}
\makeatletter
\let\stripdim\strip#pt % User interface for \strip#pt
\makeatother
\begin{document}
\newsavebox{\mybox}
\savebox{\mybox}{Hello World!}
\begin{picture}(\stripdim\wd\mybox,\stripdim\ht\mybox)
\put(0,0){Hello world}
\end{picture}
\end{document}

Resources