Jinja2 if value less than zero - latex

I am using Python and Jinja2 to generate a LaTeX table. The following code produces the bottom row of the table:
<snip>
\hline
\BLOCK{for col1, col2, col3, col4, col5, col6 in shareCompSum}
\VAR{col1} & \VAR{col2} & \VAR{col3}\euro & \VAR{col4}\euro & \VAR{col5}\euro & \VAR{col6}\euro \\
\BLOCK{endfor}
\hline
<snip>
The corresponding LaTeX-output is as follows:
\hline
Profit/Loss & & -174.040\euro & -9.040\euro & -723.20\euro & -733.28\euro \\
\hline
For columns 3 to 6, I would like the text color to be changed to red if the value of variable col3 is negative, and normal black if positive.
I am trying to write a statement along the following lines:
{% if col3 < 0 %} \textcolor{red}{\VAR{col3}\euro}
{% else %} \VAR{col3}\euro {% endif %}
It seems that Jinja does evaluate the if condition, but as a string-test rather than on numbers.
How can I perform the evaluation of float/numbers in Jinja?

The variable col3 appears as a string. You can set it to float via the following syntax:
{% if col3|float < 0.0 %} \textcolor{red}{\VAR{col3}\euro}
{% else %} \VAR{col3}\euro {% endif %}
The above Jinja code will typeset the variable col3 in red if negative, and black when larger than or equal to zero

Related

Problem when using multiple multirow commands in a table

I am trying to create a three columns table with a format
like this.
Naturally, the description is longer than the name column. Additionally, I want to use \multicolumn command for the example column for aesthetics reason. Here is the code,
\begin{document}
\begin{table}
\begin{tabular}{|c|p {5 cm}|p {5 cm}|}
\hline
\multirow{2}{*}{Complex Type} & \multirow{2}{5 cm}{\parbox[c]{5 cm}{This variable type is used to declare a complex number, the real part and also the imaginary part.}} & \multicolumn{1}{l|}{Defining a complex number 3.0 + 5.0 i :} \\
& & \multicolumn{1}{l|}{complex :: a = (3.0, 5.0)} \\
\hline
\multirow{4}{*}{Character Type} & \multirow{4}{4 cm}{This variable type is used to store one character by default. It can be used to store string or multiple characters using the len modifier. The len modifier works exactly the same as kind modifier. The example is on how to declare two variables, var1 for a character and var 2 for a sentence holder.} & character :: var1 \\
& & character (len = 40) :: var2 \\
& & var1 = "A" \\
& & var2 = "How do you turn this on?" \\
\hline
\end{tabular}
\end{tabular}
I apologize if the code is too long. It seems the problem is because the entire row cell's height is not adjusted for the highest cell. It is fixed based on the first column instead. I have tried to try several method and nothing works. Any suggestions?
It looks like you are trying to use these complicate constructs with \multicolumn and \multirow just to change the alignment and add line breaks, this can be done much more easily:
\documentclass{article}
\usepackage{geometry}
\usepackage{multirow}
\usepackage{array}
\begin{document}
\begin{table}
\begin{tabular}{|c|m{5cm}|>{\raggedright\arraybackslash}m{6.2cm}|}
\hline
Complex Type &
This variable type is used to declare a complex number, the real part and also the imaginary part. &
Defining a complex number 3.0 + 5.0 i : \linebreak
complex :: a = (3.0, 5.0) \\
\hline
Character Type &
This variable type is used to store one character by default. It can be used to store string or multiple characters using the len modifier. The len modifier works exactly the same as kind modifier. The example is on how to declare two variables, var1 for a character and var 2 for a sentence holder. &
character :: var1 \linebreak
character (len = 40) :: var2 \linebreak
var1 = "A" \linebreak
var2 = "How do you turn this on?" \\
\hline
\end{tabular}
\end{table}
\end{document}

Typesetting the numbered mathematical formula in latex

How to typeset the following numbered mathematical formula:
Use amsmath's cases environment:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\Delta(x) = \begin{cases}
1 & \text{if $x > 0$} \\
-1 & \text{if $x < 0$} \\
0 & \text{otherwise}
\end{cases}
\]
\end{document}
Here is one way to do it:
$
\Delta(x)=
\left\{
\begin{matrix}
1 & \mathrm{if}~x>0 \\
-1 & \mathrm{if}~x<0 \\
0 & \mathrm{otherwise}
\end{matrix}
\right.
$
Which produces the equation you wanted like this:
The example LaTeX equation
This type of equation has a few key tricks in it:
The left bracket has to be declared as \{ in order for LaTeX to
know to display it since it is a special character
Also, the left brace has to be resized to match however many rows you include in your equation, which is done by the \left command preceding it. This can also be done for (, [, etc.
Since you have a \left command, it has to be accompanied by a \right}, but you don't want to display the right brace, so the \right. ends the section without displaying the right brace.
The \mathrm command turns the text inside it back to normal text (removing the math mode temporarily) so that your "if" and "otherwise" aren't italicized.
The ~ just adds a little space so that your "if" doesn't collide with the x > 0, which often happens when using mathrm
Alternatively, you can check out this post for other ways to write piecewise functions in LaTeX. I hope this helps.

Increment value in locomotivecms liquid markup

I wouldn't have come here if I hadn't tried many different approaches...
Obviously collections of objects do have a field size/length, but the components don't have an index. I want something like this:
{% for product in contents.products %}
<h3>Produkt {{ product.index + 1 }}</h3>
<p>{{ product.price | concat: ' €' }}</p>
{% endfor %}
I have tried the following as documented here:
http://www.omniref.com/ruby/gems/locomotivecms-liquid/classes/Liquid::Increment
{% increment variable %}
Doesn't work. I have to work in the backend editor which complains about bad syntax. Unknown tag increment. Could I be working with an old version? Unfortunately I can't check it.
I also tried assigning a value to 0 before the for loop:
{% assign x = 0 %}
And then manually increment it by 1:
{% assign x = x + 1 %}
There must be way! I mean this is basic programming. Has anybody found a way around this?
Thanks!
You can do increment in Locomotive CMS this way:
{% assign counter = 0 %}
{% capture counter %}{{ counter | plus: 1 }}{% endcapture %}

Iteration in LaTeX

I would like to use some iteration control flow to simplify the following LaTeX code.
\begin{sidewaystable}
\caption{A glance of images}
\centering
\begin{tabular}{| c ||c| c| c |c| c|| c |c| c|c|c| }
\hline
\backslashbox{Theme}{Class} &\multicolumn{5}{|c|}{Class 0} & \multicolumn{5}{|c|}{Class 1} \\
\hline
\hline
1 &
\includegraphics[scale=2]{../../results/1/0_1.eps}
&\includegraphics[scale=2]{../../results/1/0_2.eps}
&\includegraphics[scale=2]{../../results/1/0_3.eps}
&\includegraphics[scale=2]{../../results/1/0_4.eps}
&\includegraphics[scale=2]{../../results/1/0_5.eps}
&\includegraphics[scale=2]{../../results/1/1_1.eps}
&\includegraphics[scale=2]{../../results/1/1_2.eps}
&\includegraphics[scale=2]{../../results/1/1_3.eps}
&\includegraphics[scale=2]{../../results/1/1_4.eps}
&\includegraphics[scale=2]{../../results/1/1_5.eps} \\
\hline
... % similarly for 2, 3, ..., 22
\hline
23 &
\includegraphics[scale=2]{../../results/23/0_1.eps}
&\includegraphics[scale=2]{../../results/23/0_2.eps}
&\includegraphics[scale=2]{../../results/23/0_3.eps}
&\includegraphics[scale=2]{../../results/23/0_4.eps}
&\includegraphics[scale=2]{../../results/23/0_5.eps}
&\includegraphics[scale=2]{../../results/23/1_1.eps}
&\includegraphics[scale=2]{../../results/23/1_2.eps}
&\includegraphics[scale=2]{../../results/23/1_3.eps}
&\includegraphics[scale=2]{../../results/23/1_4.eps}
&\includegraphics[scale=2]{../../results/23/1_5.eps} \\
\hline
\end{tabular}
\end{sidewaystable}
I learn that the forloop package provides the for loop. But I am not sure how to apply it to my case? Or other methods not by forloop?
If I also want to simply another similar case, where the only difference is that the directory does not run from 1, 2, to 23, but in some arbitrary order such as 3, 2, 6, 9,..., or even a list of strings such as dira, dirc, dird, dirb,.... How do I make the LaTeX code into loops then?
You may use pgffor package, a tool provided by pgf. The basic syntax is:
\foreach \n in {0,...,22}{do something}
Notably, this for loop is not restricted to integers, for example:
\foreach \n in {apples,burgers,cake}{Let's eat \n.\par}
Something like this will take care of the body of your tabular:
\newcounter{themenumber}
\newcounter{classnumber}
\newcounter{imagenumber}
\forloop{themenumber}{1}{\value{themenumber} < 24}{
% \hline <-- Error here
\arabic{themenumber}
\forloop{classnumber}{0}{\value{classnumber} < 2}{
\forloop{imagenumber}{1}{\value{imagenumber} < 6}{
& \includegraphics[scale=2]{
../../results/\arabic{themenumber}/\arabic{classnumber}_\arabic{imagenumber}.eps
}
}
}
\\
\hline
}
I had to comment out the first \hline because it gave me an error:
You can't use `\hrule' here except with leaders.
I'm not sure what that means; if you really cannot live without the double line, I can look into it more.
Also note that you have to use <; for example, <= 24 will not work.
As to your update: I would simply declare a command that takes the argument that you're looping over. Something like this:
\newcommand\fordir[1]{do something complex involving directory named #1}
\fordir{dira}
\fordir{dirb}
\fordir{dirc}
\dots

lscape and supertabular in Latex

I would like to put pictures into a supertabular table within lscape enviroment. The code is:
\newcounter{themenumber}
\newcounter{classnumber}
\newcounter{imagenumber}
\tablefirsthead{
\hline
\backslashbox{Concept}{Class} &\multicolumn{3}{|c|}{Class 0} & \multicolumn{3}{|c|}{Class 1} \\ %\textbf{A} & \textbf{B}\\
\hline}
\tablehead{
\hline
\multicolumn{7}{|l|}{\small\sl continued from previous page}\\
\hline
\backslashbox{Concept}{Class} &\multicolumn{3}{|c|}{Class 0} & \multicolumn{3}{|c|}{Class 1} \\ %\textbf{A} & \textbf{B}\\
\hline}
\tabletail{
%\hline
\multicolumn{7}{|l|}{\small\sl continued on next page}\\
\hline}
\tablelasttail{} %\tablelasttail{\hline}
\begin{landscape}
\begin{supertabular}{| c || c | c | c || c | c | c |}
\topcaption{Examples of All the Concepts. \label{tab:conceptsimgs}}
\forloop{themenumber}{1}{\value{themenumber} < 24}{
\arabic{themenumber}
\forloop{classnumber}{0}{\value{classnumber} < 2}{
\forloop{imagenumber}{1}{\value{imagenumber} < 4}{
& \includegraphics[scale=0.5]{../\arabic{themenumber}/\arabic{classnumber}_\arabic{imagenumber}.eps}
}
}
\\
\hline
}
\end{supertabular}
\end{landscape}
However there is something wrong with the result: no caption is shown, the height of the part of table in each page exceeds the page height and there is something extra unwanted at the last page. See images below:
page1 page2 page3 page4
How to fix the problems? Thanks and regards!
Update:
All the problems remain when forloop is removed and replaced with:
1 &
\includegraphics[scale=0.5]{../1/0_1.eps}
&\includegraphics[scale=0.5]{../1/0_2.eps}
&\includegraphics[scale=0.5]{../1/0_3.eps}
&\includegraphics[scale=0.5]{../1/1_1.eps}
&\includegraphics[scale=0.5]{../1/1_2.eps}
&\includegraphics[scale=0.5]{../1/1_3.eps} \\
\hline
... % from 2 to 22
23 &
\includegraphics[scale=0.5]{../23/0_1.eps}
&\includegraphics[scale=0.5]{../23/0_2.eps}
&\includegraphics[scale=0.5]{../23/0_3.eps}
&\includegraphics[scale=0.5]{../23/1_1.eps}
&\includegraphics[scale=0.5]{../23/1_2.eps}
&\includegraphics[scale=0.5]{../23/1_3.eps} \\
\hline
Any idea how to fix the problems? Thanks!
Might be the same problem as forloop and table in LaTeX
Try to create a table without forloop — just copy-paste the same line enough times. If the problem disappears, it's most probably the same one.

Resources