LaTeX align word characters to the right - pdf

I made a latex document with a line next to the margin
\documentclass[12pt]{article}
\usepackage[doublespacing]{setspace}
\usepackage[left=0.95in,top=1in,right=1in,bottom=0.75in]{geometry}
\usepackage{background}
\pagenumbering{gobble}
\SetBgScale{1}
\SetBgColor{black}
\SetBgAngle{0}
\SetBgHshift{0pt}
\SetBgVshift{0mm}
\SetBgContents{
\hspace{1in}
\rule{1pt}{\paperheight} % right first line
\rule[0.75in]{6.5in}{1pt} % bottom line
\rule{1pt}{\paperheight}
}
\setlength{\marginparwidth}{3.0in}
\begin{document}
\reversemarginpar{\vspace{1em}
\begin{spacing}{1.6} %space vertical between numbers
\noindent Sam \\ Rams\\ Tamim \\ Smartcoi \\ 9d5 \\ lousy99\\
\end{spacing}}
\end{document}
How do I get the characters on the words to align right and end at the line? Currently it renders like this:
I am trying to get the end of the words to line up with the line. I tried \begin{flushright} but it moved everything out of place

One possible approach is to use a tabular:
\documentclass[12pt]{article}
\usepackage[doublespacing]{setspace}
\usepackage[left=1.5in,top=1in,right=0.5in,bottom=0.75in,showframe]{geometry}
\usepackage{lipsum}
\pagenumbering{gobble}
\begin{document}
\reversemarginpar%
\marginpar{%
\begin{tabular}{#{}r#{}}
Sam \\
Rams\\
Tamim \\
Smartcoi \\
9d5 \\
lousy99\\
\end{tabular}%
}
\lipsum
\end{document}

Related

Inserting a text file between 2 headings

I was using this to replace and insert text between ! Heading 1 and ! Heading 2, Was working previously.
Have I done something wrong here?
value=`cat sample3`
echo "$value"
# remove chars between headings
sed '/! Heading 1,/! Heading 2/{//p;d;}' $file.txt > file-trimmed.txt
# insert value between the 2 headings
awk -v value="$value" '
$0 == "! Heading 1" {
replace = 1
print
next
}
$0 == "! Heading 2" {
replace = 0
print
next
}
replace {
sub(/^..../, value)
}
{ print }
' file-trimmed.txt > file-mod.txt
This might work for you (GNU sed):
sed -e '/! Heading 1/{p;r sample3' -e ':a;N;/! Heading 2/!s/\n//;ta;D}' file
If a line contains ! Heading 1, print it and then print the contents of sample3.
Then gather up any lines (reducing those lines to a single line) until another line containing ! Heading 2 and delete the collected lines i.e. the first of two.
An alternative perhaps more elegant solution:
sed -ne '/! Heading 1/{p;r sample3' -e ':a;n;/! Heading 2/!ba};p' file

printing repeated parts of file with awk

I would like to go through this file:
\chapter{CHAPTER}
TEXT
\e
h454
\e
\e
454
\e
\begin{figure}
\NOTE{figure}
\centering
\includegraphics[width=0.49\textwidth]{f.pdf}
\caption{\NOTEB{The concept}}
\label{fig}
\end{figure}
SOME TEXT
\e
454
\e
SOME TEXT
\begin{figure}
\NOTE{figure}
\centering
\includegraphics[width=0.49\textwidth]{f.pdf}
\caption{\NOTEB{The concept}}
\label{fig}
\end{figure}
\chapter{CHAPTER}
SOME TEXT
and print some parts:
awk '/\\begin\{figure\}/,/\\end\{figure\}/' file.tex
awk '/\\e/,/\\e/' file.tex
awk '/\chapter/' file.tex
but all into one file and in the order as in the input file. So, the desired output is (empty line does't matter):
\chapter{CHAPTER}
\e
h454
\e
\e
454
\e
\begin{figure}
\NOTE{figure}
\centering
\includegraphics[width=0.49\textwidth]{f.pdf}
\caption{\NOTEB{The concept}}
\label{fig}
\end{figure}
\e
454
\e
\begin{figure}
\NOTE{figure}
\centering
\includegraphics[width=0.49\textwidth]{f.pdf}
\caption{\NOTEB{The concept}}
\label{fig}
\end{figure}
\chapter{CHAPTER}
How to connect these commands and make it to follow the order of input file?
Could you please try following.
awk '
/\\label/{
next
}
/\\begin\{figure\}|\\beq/{
found=1
}
found;
/\\end\{figure\}|\\eeq/{
found=""
}
/chapter/
' Input_file
Since I have written this on my cell I haven't tested it, please feel free to comment on any suggestions.

sed multiline match combined with delete between two lines?

Can I use sed's multiline match logic to delete all the lines between between a range of lines?
I have been trying all sorts of combinations of expressions with no luck.
Here is an example file ('creatures.txt'):
START TAG
species: swallowtail butterfly flying
legs
wings
head
END TAG
START TAG
species: common lizard running
legs
tail
head
END TAG
START TAG
species: peacock butterfly resting
legs
wings
head
END TAG
START TAG
species: blackbird flying
legs
wings
head
END TAG
I want do perform the following:
Whenever i encounter the following multiline match: (i) 'START' contained somewhere in the first line and (ii) 'butterfly' contained somewhere in the 2nd line
then delete all the lines between the 'START TAG' and 'END TAG' lines.
So using the example file above, the resulting output would be:
START TAG
END TAG
START TAG
species: common lizard
legs
tail
head
END TAG
START TAG
END TAG
START TAG
species: blackbird
legs
wings
head
END TAG
Thanks,
James.
Like this:
sed '/START TAG/{N;/butterfly/{:a;/END TAG/!{N;ba};d}}' file
Explanation:
# Enter block when 'START TAG' is found
/START TAG/ {
N # Append next line to the pattern buffer
# Enter block when 'butterfly' is found
/butterfly/ {
:a # Create a label (could be also :foo)
# Enter block unless 'END TAG' is found
/END TAG/! {
N # Append next line to pattern buffer
ba # branch back to label :a
}
# Once 'END TAG' is found
d # Delete the pattern buffer
}
}
PS: A slightly modified version can be used to keep the START / END tags:
sed '/START/{p;N;/butterfly/{:a;/END/!{N;ba};s/.*\n//}}' file
Any time you find yourself saying sed multiline you are looking at the wrong tool. sed is for doing s/old/new on individual lines, that is all. For anything else you should be using awk.
$ cat tst.awk
{ rec = (rec=="" ? "" : rec ORS) $0 }
/END/ {
numLines = split(rec,lines,ORS)
print (lines[2] ~ /butterfly/ ? lines[1] ORS lines[numLines] : rec)
rec = ""
}
$ awk -f tst.awk file
START TAG
END TAG
START TAG
species: common lizard running
legs
tail
head
END TAG
START TAG
END TAG
START TAG
species: blackbird flying
legs
wings
head
END TAG
The above will work using any awk in any shell on any UNIX box, is clear, simple, robust and easily modified if/when your requirements change later to look at or print any of the other lines in each record or any combinations of values in each line or across the whole record. For example, to generate a CSV of just records where the creature has wings:
$ cat tst.awk
{ rec = (rec=="" ? "" : rec ORS) $0 }
/END/ {
numLines = split(rec,lines,ORS)
if ( lines[4] == "wings" ) {
sub(/species: /,"",lines[2])
for (i=2; i<numLines; i++) {
printf "%s%s", lines[i], (i<(numLines-1) ? "," : ORS)
}
}
rec = ""
}
$ awk -f tst.awk file
swallowtail butterfly flying,legs,wings,head
peacock butterfly resting,legs,wings,head
blackbird flying,legs,wings,head
and anything else you might want to do is equally trivial and consistent.

Need help in understanding the code below awk(&&&&) code:

#!/bin/awk -f
{
if (length($0) < 80)
{
prefix = "";
for (i = 1;i<(80-length($0))/2;i++)
prefix = prefix " ";
print prefix $0;
}
else
{
print;
}
}
Could any one please tell me what exactly the prefix variable is doing in the above code.
This is to make the incoming text as Centre Aligned text.
Read the text
Declare a empty string in the variable name prefix
Calculate the position where to paste your text is determined by the for loop by prefixing spaces for the same. In this case, we print spaces until we are at the position at ((80 - length of your string ) /2)
Print your string
Note: $0 in AWK is your complete string like "I want to test this" where as $1 will be "I" and $2 will be "want", where as in shell it prints your current shell you are working with
It's adding front padding to center the string on the line if it's shorter than the line length but you can do the same thing with just:
awk '{ printf "%*s\n",(80+length($0))/2, $0 }' file
It increments prefix with blank space to create a line with space in front according to the formula.
echo "test" | awk -f script
test
it builds an empty string place holder (for left padding), which has length= (80-length of the line)/2

Lex : line with one character but spaces

I have sentences like :
" a"
"a "
" a "
I would like to catch all this examples (with lex), but I don't how to say the beginning of the line
I'm not totally sure what exactly you're looking for, but the regex symbol to specify matching the beginning of a line in a lex definition is the caret:
^
If I understand correctly, you're trying to pull the "a" out as the token, but you don't want to grab any of the whitespace? If this is the case, then you just need something like the following:
[\n\t\r ]+ {
// do nothing
}
"a" {
assignYYText( yylval );
return aToken;
}