LaTeX: Use some characters in a string - latex

I need a macro that extracts pairs of number from a string that looks like this:
n1-m1,n2-m2,n3-m3,n4-m4 (it could be longer)
where n1,m1,n2,m2,... are numbers from 0 - 15. How can I go about getting the pairs (n1,m1), and (n2,m2), (n3,m3), etc inside my macro? I will need to use each pair once, after which I can, if needed, disregard the pair.
Assuming each digit is a 2-digit number (not an elegant thing to do), and butchering a code I found by Debilski in this forum, I managed to get the first pair doing the following:
\documentclass[11pt]{article}
\def\macroGetPairs #1{\getPairs#1.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {
\if#1.%
\else
% Test if pair was successfully extracted
Got pair (#1#2,#3#4). Still left: #5\\
% Begin recursion
%\takeTheRest#5\ofTheString
\fi}
\def\takeTheRest#1\ofTheString\fi
{\fi \getPairs#1\wholeString}
\begin{document}
\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}
However, I am not sure how to get the recursion working for me to get the rest of the pairs. I thought that simply uncommenting the line
%\takeTheRest#5\ofTheString
should do it, but it does not work. Note that the macro's test call is:
\macroGetPairs{10-43,40-51,60-73,83-97}
Any suggestions? Thank you very much,
ERM

This seems to get your test to work:
\documentclass{article}
\def\macroGetPairs#1{\getPairs#1,.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {%
Got pair (#1#2,#3#4).\\
\if#5.\else%
\getPairs#5\wholeString
\fi}
\begin{document}
\noindent\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}
Your code was basically working, but there was no way for \getPairs to match its input on the final expansion (\getPairs 83-97). Your end-of-recursion test (\if#1.) was also testing #1 rather than #5, which is what I've done here. Maybe if there was some different way of formatting the argument to \getPairs that would have worked.

Related

How do I use \renewcommand to get BACK my greek letters?

I'm a LaTeX newbie, but I've been doing my homework, and now I have a question that I can't seem to find the answer to.
I create the definition of an equation, let's just say it's this one:
The potential is characterized by a length $\sigma$ and an energy $\epsilon$.
In reality, this equation is more complex, which is why I wanted to try a shortcut. If my equation were this simplistic, I wouldn't try my substitution technique.
I use the \renewcommand to save me some time:
\renewcommand{\sigma}{1}
And this works fabulously and will replace all instances of sigma with 1. Unfortunately though, since \sigma has a global scope, I need to reset it.
I tried a couple different ways:
Attempt 1: -deadlock due to circular reference?
\newcommand{\holdsigma}{\sigma}
\renewcommand{\sigma}{1}
The potential is characterized by a length $\sigma$ and an energy $\epsilon$.
\renewcommand{\sigma}{\holdsigma}
I would think to reset the command, it should look something like this:
\renewcommand{\sigma}{\greek{\sigma}}
but that obviously hasn't worked out for me.
Any idea about how the greek letters are originally defined in the language?
I have to admit that I don't understand why you want to do what you're asking, but this should work:
\documentclass{article}
\begin{document}
Before redefinition, \verb|\sigma| looks like $\sigma$.
% Copy the current definition of \sigma to \oldsigma
\let\oldsigma\sigma
% Redefine \sigma to be '1'
\renewcommand{\sigma}{1}
After redefinition, \verb|\sigma| looks like $\sigma$.
You can still use \verb|\oldsigma| if you want to use the original definition $\oldsigma$.
% Restore the original definition of \sigma
\let\sigma\oldsigma
Now \verb|\sigma| is back to its normal appearance $\sigma$.
\end{document}
To find out how \sigma or any other command is originally defined, you can use \show\sigma. (The answer is that \sigma is defined as \mathchar"11B.) You can type this either in your document itself — compilation will pause and you can type Enter after reading the reply — or you can type this in TeX/LaTeX's interactive mode.
Example with a document:
\documentclass{article}
\begin{document}
What is $\sigma$? % Prints "What is σ" in the DVI/PS/PDF.
\show\sigma % Prints "> \sigma=\mathchar"11B." in the compilation.
Now that we know, let us redefine it.
\renewcommand{\sigma}{1}
Now it is: $\sigma$. % Prints "Now it is: 1." in the DVI/PS/PDF.
OK, let's go back.
\renewcommand{\sigma}{\mathchar"11B}
We again have: $\sigma$. %Prints "We again have: σ." in the DVI/PS/PDF.
\end{document}
Or else at the command prompt, type latex, then type \relax, then type \show\sigma, read what it says, then type x to exit.

Controlling LaTeX column flow

What I'm trying to do: I have a page that consists of pairs of two sentences each. The pairs are separated by a whole line break. My problem is that when I have an odd number of pairs, the second sentence will automatically be placed on the next column.
How can I use LaTeX to make block structures that multicol does not ignore, to keep the two sentences together? If there's better code to solve this problem, or a better column implementation (though I don't believe I can use \twocolumn in the document declaration), please post it.
My current code:
\documentclass{article}
\usepackage{fullpage}
\usepackage{multicol}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\newcommand{\pair}[2]{
\emph{#1}\\*
#2
}
\begin{document}
\begin{multicols}{2}
\pair{Sentence 1.}{Sentence 2.}
\pair{Sentence 2 (pair 2).}{Sentence 2 (pair 2).}
\pair{The last pair, first sentence.}{Last sentence.}
\end{multicols}
\end{document}
This generates: http://img541.imageshack.us/img541/3444/columns.png . The second pair is what I am trying to avoid.
Try this:
\newcommand{\pair}[2]{%
\parbox{\hsize}{\emph{#1}\\*#2}\par}

LaTeX \newcommand default argument: is empty?

I'm trying to write a simple example command that prints nothing without an argument, but with an argument it surrounds it with something.
I've read that the default value should be \#empty and the simple \ifx\#empty#1 condition should do the job:
\newcommand{\optarg}[1][\#empty]{%
\ifx\#empty#1 {} \else {(((#1)))} \fi
}
\optarg % (((empty)))
\optarg{} % (((empty)))
\optarg{test} % (((empty))) test
The latter three commands all print the empty word for some reason, and I want the first two to print nothing and the last to print (((test))).
I'm using TeXLive/Ubuntu. An ideas?
Try the following test:
\documentclass{article}
\usepackage{xifthen}% provides \isempty test
\newcommand{\optarg}[1][]{%
\ifthenelse{\isempty{#1}}%
{}% if #1 is empty
{(((#1)))}% if #1 is not empty
}
\begin{document}
Testing \verb|\optarg|: \optarg% prints nothing
Testing \verb|\optarg[]|: \optarg[]% prints nothing
Testing \verb|\optarg[test]|: \optarg[test]% prints (((test)))
\end{document}
The xifthen package provides the \ifthenelse construct and the \isempty test.
Another option is to use the ifmtarg package (see the ifmtarg.sty file for the documentation).
Using the LaTeX3 xparse package:
\usepackage{xparse}
\NewDocumentCommand\optarg{g}{%
\IfNoValueF{#1}{(((#1)))}%
}
In the underlying TeX engine with which LaTeX is written, the number of arguments a command can take is fixed. What you've done with the default [\#empty] is ask LaTeX to examine the next token to see if it is an open square bracket [. If so, LaTeX takes the contents of square brackets as the argument, if not, the next token is put back into the input stream and the default \#empty argument is used instead. So to get your idea to work, you have to use square brackets to delimit the optional argument when present:
\optarg
\optarg[]
\optarg[test]
You should have better luck with this notation.
It's annoying that you can't use the same brackets for an optional argument as you use for a required argument, but that's the way it is.
\documentclass{article}
\usepackage{ifthen} % provides \ifthenelse test
\usepackage{xifthen} % provides \isempty test
\newcommand{\inlinenote}[2][]{%
{\bfseries{Note:}}%
\ifthenelse{\isempty{#1}}
{#2} % if no title option given
{~\emph{#1} #2} % if title given
}
\begin{document}
\inlinenote{
simple note
}
\inlinenote[the title]{
simple note with title
}
\end{document}

Latex listings-package format option for uppercase keywords

I use the listings package to insert source code. I would like to print all keywords uppercase in the output, regardless of the case in the input.
The manual states that
keywordstyle=[number][*]style
produces just what I want. However the following (almost) minimal example does not work.
if I set keywordstyle to "[1][]{\bfseries}" I end up with "[]" in front of every keyword
and "[*]{\bfseries}" gives me an asterisk in the start of the document.
I also tried "\MakeUppercase" and "{\MakeUppercase}" for keywordstyle which resulted in several errors, the first being:
! Incomplete \iffalse; all text was ignored after line 11
Minimal example:
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{KA_assembler}
{morekeywords={add,and,or,xor},
keywordstyle=[1][*]{\bfseries},
sensitive=false,
}
\lstset{language=KA_assembler}
\begin{document}
\begin{lstlisting}
and %r1, %r2
xor %r2, %r3
and %r4, %r5
\end{lstlisting}
\end{document}
I use Miktex for compilation of the tex files. So how do I force uppercase for Keywords?
In the manual, the brackets around the * look a bit different then the brackets around number. The reason is that the brackets around * are not meant to be used in the latex code, they just indicate that the presence of the * is optional. So try
keywordstyle=[1]*\bfseries
or
keywordstyle=*\bfseries
- it worked for me.

How to put line break in a math

I'd like to express the following sentence (source_location is also italic, it's not correctly rendered):
Each entry has a list of tuple: < source_location, R/W, trip_counter, occurrence, killed (explained in the later) >
My current workaround for this is:
$ \left\langle
\textit{source\_location}, \textit{R/W}, \textit{trip\_counter},
\textit{occurrence}, \textit{killed} \text{(explained in the later)}
\right\rangle $
I'm using 2-column paper. This < .. > is too long, but no line break because it is a math. How do I automatically (or manually) put line break in such case? It seems that \left\langle and \right\rangle should be in a single math. So, hard to break into multiple maths.
$<$ and $>$ would be an alternative, but I don't like it.
LaTeX does allow inline maths to break over lines by default, but there are a number of restrictions. Specifically, in your case, using \left...\right puts everything inside a non-breakable math group, so the first step is to replace them with either just plain \langle...\rangle or perhaps \bigl\langle...\bigr\rangle.
However, this still isn't enough to permit linebreaking; usually that's still only allowed after relations or operators, not punctuation such as the comma. (I think this is what's going on anyway; I haven't stopped to look this up.) So you want indicate where allowable line breaks may occur by writing \linebreak[1] after each comma.
Depending how often you have to do this, it may be preferable to write a command to wrap your "tuples" into a nice command. In order to write this in your source:
$ \mytuple{ source\_location, R/W, trip\_counter, occurrence,
killed\upshape (explained in the later) } $
here's a definition of \mytuple that takes all of the above into account:
\makeatletter
\newcommand\mytuple[1]{%
\#tempcnta=0
\bigl\langle
\#for\#ii:=#1\do{%
\#insertbreakingcomma
\textit{\#ii}
}%
\bigr\rangle
}
\def\#insertbreakingcomma{%
\ifnum \#tempcnta = 0 \else\,,\ \linebreak[1] \fi
\advance\#tempcnta\#ne
}
\makeatother
Why not define a new command:
\newcommand{\tuple}[5]{$\langle$\textit{#1}, \textit{#2}, \textit{#3}, \textit{#4},
\textit{#5} (explained in the latter)$\rangle$}
Then use \tuple{sourcelocation}{R/W}{tripcounter}{occurrence}{killed}
There seems to be a package that addresses that problem, called breqn. You can try this and let us know (I haven't used that).
I'd use the align* environment from AMSmath. Furthermore you could just add "\" to break the lines? Should work in math environments, too. Alternatively you could separate the equations.
Use \linebreak inside the math expression wherever you want a new line even between 2 brackets. This will enforce the line to be broken.

Resources