Latex nested environment: align, linenomath, split - nested

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}

Related

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}

Jade: Links inside a paragraph

I'm trying to author a few paragraphs with Jade, but finding it difficult when there are links inside a paragraph.
The best I can come up with, and I'm wondering if there's a way to do it with less markup:
p
span.
this is the start
of the para.
a(href="http://example.com") a link
span.
and this is the rest of
the paragraph.
As of jade 1.0 there's an easier way to deal with this, unfortunately I can't find it anywhere in the official documentation.
You can add inline elements with the following syntax:
#[a.someClass A Link!]
So, an example without going into multiple lines in a p, would be something like:
p: #[span this is the start of the para] #[a(href="http://example.com") a link] #[span and this is the rest of the paragraph]
You can also do nested inline elements:
p: This is a #[a(href="#") link with a nested #[span element]]
You can use a markdown filter and use markdown (and allowed HTML) to write your paragraph.
:markdown
this is the start of the para.
[a link](http://example.com)
and this is the rest of the paragraph.
Alternatively it seems like you can simply ouput HTML without any problems:
p
| this is the start of the para.
| a link
| and this is he rest of the paragraph
I wasn't aware of this myself and just tested it using the jade command line tool. It seems to work just fine.
EDIT:
It seems it can actually be done entirely in Jade as follows:
p
| this is the start of the para
a(href='http://example.com;) a link
| and this is the rest of the paragraph
Don't forget an extra space at the end of para (although you can't see it. and between | and. Otherwise it will look like this para.a linkand not para a link and
Another way to do it:
p
| this is the start of the para
a(href="http://example.com") a link
|
| this is the rest of the paragraph.
Another completely different approach, would be to create a filter, which has first stab at replacing links, and then renders with jade second
h1 happy days
:inline
p this can have [a link](http://going-nowhere.com/) in it
Renders:
<h1>happy days</h1><p>this can have <a href='http://going-nowhere.com/'>a link</a> in it</p>
Full working example: index.js (run with nodejs)
var f, jade;
jade = require('jade');
jade.filters.inline = function(txt) {
// simple regex to match links, might be better as parser, but seems overkill
txt = txt.replace(/\[(.+?)\]\((.+?)\)/, "<a href='$2'>$1</a>");
return jade.compile(txt)();
};
jadestring = ""+ // p.s. I hate javascript's non-handling of multiline strings
"h1 happy days\n"+
":inline\n"+
" p this can have [a link](http://going-nowhere.com/) in it"
f = jade.compile(jadestring);
console.log(f());
A more general solution would render mini sub-blocks of jade in a unique block (maybe identified by something like ${jade goes here}), so...
p some paragraph text where ${a(href="wherever.htm") the link} is embedded
This could be implemented in exactly the same way as above.
Working example of general solution:
var f, jade;
jade = require('jade');
jade.filters.inline = function(txt) {
txt = txt.replace(/\${(.+?)}/, function(a,b){
return jade.compile(b)();
});
return jade.compile(txt)();
};
jadestring = ""+ // p.s. I hate javascript's non-handling of multiline strings
"h1 happy days\n"+
":inline\n"+
" p this can have ${a(href='http://going-nowhere.com/') a link} in it"
f = jade.compile(jadestring);
console.log(f());
If your links come from a data source you can use:
ul
each val in results
p
| blah blah
a(href="#{val.url}") #{val.name}
| more blah
See interpolation
Edit: This feature was implemented and issue closed, see answer above.
I've posted an issue to get this feature added into Jade
https://github.com/visionmedia/jade/issues/936
Haven't had time to implement it though, more +1s may help !
This is the best I can come up with
-var a = function(href,text){ return "<a href='"+href+"'>"+text+"</a>" }
p this is an !{a("http://example.com/","embedded link")} in the paragraph
Renders...
<p>this is an <a href='http://example.com/'>embedded link</a> in the paragraph</p>
Works ok, but feels like a bit of a hack - there really should be a syntax for this!
I did not realize that jade requires a line per tag. I thought we can save space. Much better if this can be understood ul>li>a[class="emmet"]{text}
I had to add a period directly behind a link, like this:
This is your test [link].
I solved it like this:
label(for="eula").lbl.lbl-checkbox.lbl-eula #{i18n.signup.text.accept_eula}
| #{i18n.signup.links.eula}.
As suggested by Daniel Baulig, used below with dynamic params
| <a href=!{aData.link}>link</a>
Turns out there is (now at least) a perfectly simple option
p Convert a .fit file using Garmin Connect's export functionality.
p
| At Times Like These We Suggest Just Going:
a(ui-sref="app") HOME
|
Most simplest thing ever ;) but I was struggling with this myself for a few seconds. Anywho, you need to use an HTML entity for the "#" sign -> #
If you want to in include a link, let's say your/some email address use this:
p
a(href="mailto:me#myemail.com" target="_top") me#myemail.com

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}

Latex two captioned verbatim environments side-by-side

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}

Resources