I want to use the IF else in my code using LaTeX. But I am unable to end the if-else. I have used the ENDIF and the commands like this \newcommand\sIf[2]{ \If{#1}#2\EndIf}. I want to print the END if at the end of the if-else. The following is the code and its output.
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Calculating Speed Using GPS}\label{alg1}
\begin{algorithmic}[1]
\State $distance\gets 0 $
\State $time\gets 0 $
\State $speedGPS \gets 0 $
\If{$location.hasSpeed()$}
\State $speedGPS\gets location.getSpeed()$
\Else
\State $speedGPS \gets dist / time$
\EndIf
\State $counter \gets (counter + 1) \% data_points$
\State $speedGPS \gets speedGPS * 3.6d$
\end{algorithmic}
\end{algorithm}
\end{document}
Drop the noend option you're passing to algpseudocode:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Calculating Speed Using GPS}
\begin{algorithmic}[1]
\State $distance\gets 0 $
\State $time\gets 0 $
\State $speedGPS \gets 0 $
\If{$location.hasSpeed()$}
\State $speedGPS\gets location.getSpeed()$
\Else
\State $speedGPS \gets dist / time$
\EndIf
\State $counter \gets (counter + 1) \% data_points$
\State $speedGPS \gets speedGPS \times 3.6d$
\end{algorithmic}
\end{algorithm}
\end{document}
Related
Right now, I am working on a "Text Editor" made with Bash. Everything was going perfectly until I tested it. When I opened the file the script created, everything was jumbled up. I eventually figured out it had something to do with the cat BASHTE/* >> $file I had put in. I still have no idea why this happens. My crappy original code is below:
#!/bin/bash
# ripoff vim
clear
echo "###############################################################################"
echo "# BASHTE TEXT EDITOR - \\\ = interupt :q = quit :w = write #"
echo "# :wq = Write and quit :q! = quit and discard :dd = Delete Previous line #"
echo "###############################################################################"
echo ""
read -p "Enter file name: " file
touch .$file
mkdir BASHTE
clear
echo "###############################################################################"
echo "# BASHTE TEXT EDITOR - \\\ = interupt :q = quit :w = write #"
echo "# :wq = Write and quit :q! = quit and discard :dd = Delete Previous line #"
echo "###############################################################################"
while true
do
read -p "$lines >" store
if [ "$store" = "\\:q" ]
then
break
elif [ "$store" = "\\:w" ]
then
cat BASHTE/* >> $file
elif [ "$store" = "\\:wq" ]
then
cat BASHTE/* >> $file
rm -rf .$file
break
elif [ "$store" = "\\:q!" ]
then
rm -rf BASHTE
rm -rf $file
break
elif [ "$store" = "\\:dd" ]
then
LinesMinusOne=$(expr $lines - 1)
rm -rf BASHTE/$LinesMinusOne.txt
else
echo $store >> BASHTE/$lines.txt
# counts the number of times the while loop is run
((lines++))
fi
done
This is what I got after I typed in the alphabet:
b
j
k
l
m
n
o
p
q
r
s
c
t
u
v
w
x
y
z
d
e
f
g
h
I
This was what I inputted
a
v
c
d
e
f
g
h
I
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
\\:wq
Any help would be great, Thanks
BASHTE/* is in lexical order, so every line starting with 1 will come before every line starting with 2 and so on. That means the order of your input lines is:
1 a
10 j
11 k
12 l
...and so on...
To make the lines sort well with the * operator, you'll need to name them with leading zeros, for example:
# ...
echo $store >> BASHTE/$(printf %020d $lines).txt
# ...
I chose the %020d format because it should store any number of lines applicable for a 64-bit system, since 2 ** 64 = 18446744073709551616, which is 20 digits long.
I have a script which reads commands from a named pipe:
#! /usr/bin/env bash
host_pipe="host-pipe"
#pipe for executing commands
[ -p "$host_pipe" ] || mkfifo -m 0600 "$host_pipe" || exit 1
chmod o+w "$host_pipe"
set -o pipefail
while :; do
if read -r cmd <$host_pipe; then
if [ "$cmd" ]; then
printf 'Running: %s \n' "$cmd"
fi
fi
done
I run it and test with command:
bash -c "echo 'abcdef' > host-pipe"
bash -c "echo 'abcdef' > host-pipe"
bash -c "echo 'abcdef' > host-pipe"
bash -c "echo 'abcdef' > host-pipe"
And get the strange output:
Running: abcdf
Running: abcdef
Running: abcde
Running: abcdf
Running: ace
Somehow the script can't read all the string it get from the pipe? How to read it?
You must have more than one reader of the named pipe host-pipe running for this to happen.
Check to see if you have a second instance of the script running in the background or possibly in another terminal.
Explanation
You will find that bash will issue reads from the pipe 1 byte at a time. If you are on Linux, you can strace your script. Here is an excerpt:
open("host-pipe", O_RDONLY|O_LARGEFILE) = 3
fcntl64(0, F_GETFD) = 0
fcntl64(0, F_DUPFD, 10) = 10
fcntl64(0, F_GETFD) = 0
fcntl64(10, F_SETFD, FD_CLOEXEC) = 0
dup2(3, 0) = 0
close(3) = 0
ioctl(0, TCGETS, 0xbf99bfec) = -1 ENOTTY (Inappropriate ioctl for device)
_llseek(0, 0, 0xbf99c068, SEEK_CUR) = -1 ESPIPE (Illegal seek)
read(0, "a", 1) = 1
read(0, "b", 1) = 1
read(0, "c", 1) = 1
read(0, "d", 1) = 1
read(0, "e", 1) = 1
read(0, "f", 1) = 1
read(0, "\n", 1) = 1
dup2(10, 0) = 0
fcntl64(10, F_GETFD) = 0x1 (flags FD_CLOEXEC)
close(10) = 0
Once you have more than one process with this consumption pattern, any single process will see lost characters.
I was wondering if changing the color of the font for certain numbers in TextEdit was possible.
For exemple to have all '1's in blue, '2's red etc...
If that's impossible in TextEdit, can someone suggest a similar software able to do so and read .txt files ?
This was fun! macOS includes Perl and textutil which is used for changing document formats, so you can do something like what you want without needing to install anything...
#!/bin/bash
perl -pe 'BEGIN{ $/=\1; }
s/0/<font color="red" >0<font color="black">/ ||
s/1/<font color="green" >1<font color="black">/ ||
s/2/<font color="blue" >2<font color="black">/ ||
s/3/<font color="cyan" >3<font color="black">/ ||
s/4/<font color="magenta">4<font color="black">/ ||
s/5/<font color="yellow" >5<font color="black">/ ||
s/6/<font color="pink" >6<font color="black">/ ||
s/7/<font color="orange" >7<font color="black">/ ||
s/8/<font color="teal" >8<font color="black">/ ||
s/9/<font color="gray" >9<font color="black">/ ||
s/\n/<br>/;
' "$1" | textutil -format html -convert rtf -stdin -stdout > output.rtf
Save that as go and make it executable, just once with:
chmod +x go
Then you can convert any document like this:
./go input.txt
and it will make output.rtf.
So, if I start with the following in input.txt:
1234567890 I love RTF and HTML but not 2 much and I can't spell, or count 1,000,222
1 man had 2 dogs and 333 cats
I end up with:
The Perl reads 1 character at a time, because of $/=\1;. It then replaces any digit with an HTML font tag with a different colour, then sets the colour back to black after the digit. The output is then passed to textutil which is told to treat the input as html and make the output RTF.
I have spent all day on this and I still have no idea why one of your documents works and the other doesn't. The only thing I can say is that textutil seems not to crash when I comment out the following line that changes the colours of the 1s in your document to green! I have no idea why!
s/1/<font color="green" >1<font color="black">/ ||
I re-wrote it in a different way even, but it is still the same. I will put the code below so that others can play with it:
#!/usr/bin/env perl
use strict;
use warnings;
my ($i, $x, %HofH);
# Initialise table mapping input bytes to output/actions
# So it's a hash of hashes, each hash stores the corresponding output string and whether it set colour
foreach $i ('A'..'Z', 'a'..'z' ){
$x = ord($i);
$HofH{$x}{out} = $i;
}
# Set up the coloured digits 0-9
$i=48; $HofH{$i}{out} = sprintf("<font color='red'>0") ; $HofH{$i}{colour}=1;
#$i=49; $HofH{$i}{out} = sprintf("1") ; $HofH{$i}{colour}=1; # THIS CRASHES TEXTUTIL ???
$i=50; $HofH{$i}{out} = sprintf("<font color='blue'>2") ; $HofH{$i}{colour}=1;
$i=51; $HofH{$i}{out} = sprintf("<font color='cyan'>3") ; $HofH{$i}{colour}=1;
$i=52; $HofH{$i}{out} = sprintf("<font color='magenta'>4"); $HofH{$i}{colour}=1;
$i=53; $HofH{$i}{out} = sprintf("<font color='yellow'>5") ; $HofH{$i}{colour}=1;
$i=54; $HofH{$i}{out} = sprintf("<font color='pink'>6") ; $HofH{$i}{colour}=1;
$i=55; $HofH{$i}{out} = sprintf("<font color='orange'>7") ; $HofH{$i}{colour}=1;
$i=56; $HofH{$i}{out} = sprintf("<font color='teal'>8") ; $HofH{$i}{colour}=1;
$i=57; $HofH{$i}{out} = sprintf("<font color='grey'>9") ; $HofH{$i}{colour}=1;
# Special cases - CR, LF, ampersand
$i=9; $HofH{$i}{out} = " ";
$i=10; $HofH{$i}{out} = "<br>\n";
$i=13; $HofH{$i}{out} = "<br>\n";
$i=39; $HofH{$i}{out} = "&";
$i=47; $HofH{$i}{out} = "/";
$i=61; $HofH{$i}{out} = "=";
$i=63; $HofH{$i}{out} = "?";
$i=40; $HofH{$i}{out} = "(";
$i=41; $HofH{$i}{out} = ")";
$i=45; $HofH{$i}{out} = "-";
$i=58; $HofH{$i}{out} = ":";
$i=59; $HofH{$i}{out} = ";";
$i=46; $HofH{$i}{out} = ".";
$i=44; $HofH{$i}{out} = ",";
$i=32; $HofH{$i}{out} = " ";
$i=60; $HofH{$i}{out} = "<";
$i=62; $HofH{$i}{out} = ">";
open(my $FILE, $ARGV[0]) or die $!;
binmode($FILE);
local $/ = \1;
my $cnt=0;
while ( my $byte = <$FILE> ) {
my $x = ord($byte);
if(defined $HofH{$x}){
printf("%s",$HofH{$x}{out});
printf("<font color='black'>") if defined $HofH{$x}{colour}
} else {
printf STDERR "ERROR: Character number %d ignored at byte offset %d\n",$x,$cnt;
}
$cnt++;
}
close $FILE;
You run it with:
thisScript.pl someFile.txt | textutil -format html -convert rtf -stdin -stdout > output.rtf
I have a problem with tables being sorted in a different ways on my two ubuntu servers. I'm executing the following command in the redis-cli tool. This is the server 1:
127.0.0.1:6379> eval "local a = {'_mcat:banner','for_meta:1','_size:300x250','_mtype:html', 'axx:1'};table.sort(a);return a;" 0
1) "_mcat:banner"
2) "_mtype:html"
3) "_size:300x250"
4) "axx:1"
5) "for_meta:1"
And the server 2:
127.0.0.1:6379> eval "local a = {'_mcat:banner','for_meta:1','_size:300x250','_mtype:html', 'axx:1'};table.sort(a);return a;" 0
1) "axx:1"
2) "for_meta:1"
3) "_mcat:banner"
4) "_mtype:html"
5) "_size:300x250"
As you can see the problem lays in how the sort is treating the '_' character. I have tried to upgrade versions of redis & gcc and the behavior is not changed between the servers. Redis versions tested: 2.8.4, 2.8.19, 4.0.2, gcc version: 4.8.2. Running locale returns the same values on both servers:
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Executing the same code in Python, works the same on both machines:
>>> sorted(['_mcat:banner','for_meta:1','_size:300x250','_mtype:html', 'axx:1'])
['_mcat:banner', '_mtype:html', '_size:300x250', 'axx:1', 'for_meta:1']
Maybe somebody has a suggestion on what to try next? Thanks.
EDITED:
On both servers running:
eval "local a = {'_mcat:banner','for_meta:1','_size:300x250','_mtype:html', 'axxx:2'};table.sort(a, function(a,b) return a<b end);return a;" 0
Produces results consistent with the table.sort(a) (so different between servers), but take a look at this:
On server 1:
127.0.0.1:6379> eval "local a;if 'axxx:2'>'_mcat:banner' then a=1 else a=0 end;return a;" 0
(integer) 1
127.0.0.1:6379> eval "local a;if 'a'>'_' then a=1 else a=0 end;return a;" 0
(integer) 1
On server 2:
127.0.0.1:6379> eval "local a;if 'axxx:2'>'_mcat:banner' then a=1 else a=0 end;return a;" 0
(integer) 0
127.0.0.1:6379> eval "local a;if 'a'>'_' then a=1 else a=0 end;return a;" 0
(integer) 1
Given that table.sort is implemented in Lua itself using quicksort (src/ltablib.c) and for strings the comparison boils down to strcoll call, it's difficult to say where it could go wrong for underscores. I would try adding a custom sort function to see if this changes the outcome (table.sort(a, function(a,b) return a<b end)) and if it still produces the incorrect results, then you can print a, b, a<b values to see where it goes wrong.
Thanks for all the comments and suggestions, I still don't know what exactly happened but rebuilding the server worked.
For testing purposes, let's draw some meaningless rectangles:
gswin32c -q -o f.pdf -sDEVICE=pdfwrite -c "<</PageSize[595 842]>>setpagedevice 0 0 595 842 rectfill showpage"
+
gswin32c -q -o f.eps -sDEVICE=eps2write -f f.pdf
And ps.ps file:
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(f.eps) run
end
} if
}
>> setpagedevice
And then
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
produces an error:
%%[ Error handled by opdfread.ps : GPL Ghostscript 9.15: Unrecoverable
error, exit code 1 Unrecoverable error: typecheck in if Operand stack:
typecheck ebuf false false --nostringval--
On the other hand, if I create EPS with another tool, e.g. xpdf's pdftops:
pdftops -eps f.pdf f.eps
... then EPS can be placed as e.g. watermark or logo with above command:
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
So the question is, is it possible to use Ghostscript's eps2write for the purpose, maybe I'm missing something.
I tried to bracket (f.eps) run with calls to BeginEPSF and EndEPSF as defined in Adobe's EPSF Format Specification, but it didn't help. And after decoding prologue which eps2write creates (which, itself, gives same error if run from EndPage), it looks to me that it violates section on Illegal and Restricted Operators of mentioned Specification.
Edit:
I think there's an issue with the Immediately Evaluated Names if code is run from EndPage. In prologue, which eps2write creates, there's a fragment not very far from the beginning:
//SetPageSize{
//RotatePages//FitPages or//CenterPages or{
mark(/RotatePages, /FitPages and CenterPages are not allowed with /SetPageSize)//error exec
}if
}
{
//FitPages//CenterPages and{
mark(CenterPages is not allowed with /FitPages)//error exec
}if
}
ifelse
If I frame it like this:
SetPageSize ==
//SetPageSize ==
{
//SetPageSize{
//RotatePages//FitPages or//CenterPages or{
mark(/RotatePages, /FitPages and CenterPages are not allowed with /SetPageSize)//error exec
}if
}
{
//FitPages//CenterPages and{
mark(CenterPages is not allowed with /FitPages)//error exec
}if
}
ifelse
} stopped { (***\n) print } if
And modify ps.ps slightly:
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(prologue.ps) run
end
} if
}
>> setpagedevice
Then this command:
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
...gives this output:
false
/SetPageSize
***
%%[ Error handled by opdfread.ps : GPL Ghostscript 9.14: Unrecoverable error, exit code 1
I.e. it fails in above fragment (and for obvious reason, I think) and then fails somewhere else further down within prologue.ps.
OK so I did something similar to your expereience. I started with a simple PostScript file (testeps.ps):
%!
0 1 0 setrgbcolor
0 0 100 100 rectfill
showpage
I then ran that through Ghostscript using the eps2write device:
./gs -sDEVICE=eps2write -sOutputFile=out.eps -dCompressPages=false testeps.ps
I then constructed another test file (test.ps):
%!
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(/temp/out.eps) run
end
} if
}
>> setpagedevice
1 0 0 setrgbcolor
0 100 100 100 rectfill
showpage
and ran that through GS:
./gs test.ps
The file ran to completion, and contained appropriately coloured rectangles at the correct positions on the page.
Its possible this is something which has been fixed (you don't say what version of Ghostscript you are using). The next release (9.16) is due very shortly, or you can build iot yourself from source, I'd suggest you try it when its available.