Is there a way to extract initials from macro containing a name with latex? - latex

I am writing a cv class, and want to extract the cvauthor initials. I have declare a macro in a .cls file that should contains the author name, after getting it, i want to split it according to a certain separator and gets the first letters of each sub-string. Here is what I did, but still getting an error message.
myclass.cls
\ProvidesClass{resumecv}[2020/09/12 CV class]
\LoadClass{article}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{xstring}
\RequirePackage{soul}
\RequirePackage[absolute, overlay]{textpos}
\sodef\myspace{}{.1em}{0.5em plus0.5em}{2em plus.1em minus.1em} % User-defined letter spacing
\newcommand{\cvauthor}[1]{\renewcommand{\cvauthor}{\textsc{\myspace{#1}}}}
\newcommand{\FirstInitial}{}
\newcommand{\SecondInitial}{}
\newcommand*{\ExtractInitials}[1]{%
\StrRemoveBraces{#1}[\FirstInit]%
\StrChar{\FirstInit}{1}[\FirstInit]
\renewcommand{\FirstInitial}{\FirstInit}
\StrBehind{#1}{ }[\second]%
\StrRemoveBraces{\second}[\SecondInit]%
\StrChar{\SecondInit}{1}[\SecondInitial]
}
\newcommand*{\makeskeleton}{
\begin{textblock}{20}(0,0)
\ifthenelse{\equal{\cvauthor}{}}{}{
%\Huge\textcolor{darkYellow}{\textbf{\cvauthor}}
% \renewcommand{\name}{\cvauthor}
% \name
\ExtractInitials{\cvauthor}
\FirstInitial
\SecondInitial
}
\end{textblock}
}
my-resume.tex
\documentclass[a4paper,11pt]{myclass}
\cvauthor{John Smith} %%% whant to extract J and S
\begin{document}
\makeskeleton
\end{document}
Error
! Use of \TP#textblock doesn't match its definition.
\text#command #1->\def \reserved#a {
#1}\ifx \reserved#a \#empty \let \check#...
l.5 \makeskeleton
?
Need somebody's help.

ifthen package is missing
\ExtractInitials fails because \cvauthor saves the name with all the formatting like small caps etc. An easy workaround is to define a macro that just saves the plain name
please keep in mind that your \ExtractInitials will fail in many cases: more than two name parts, names with prefix etc. Instead of reinventing the wheel, have a look at biblatex which is very good at parsing all kinds of names
\ProvidesClass{myclass}[2020/09/12 CV class]
\LoadClass{article}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{xstring}
\RequirePackage{soul}
\RequirePackage[absolute, overlay]{textpos}
\RequirePackage{ifthen}
\sodef\myspace{}{.1em}{0.5em plus0.5em}{2em plus.1em minus.1em} % User-defined letter spacing
\newcommand{\cvauthor}[1]{\newcommand{\mycvauthor}{#1}\renewcommand{\cvauthor}{\textsc{\myspace{#1}}}}
\newcommand{\FirstInitial}{}
\newcommand{\SecondInitial}{}
\newcommand*{\ExtractInitials}[1]{%
\StrRemoveBraces{#1}[\FirstInit]%
\StrChar{\FirstInit}{1}[\FirstInit]
\renewcommand{\FirstInitial}{\FirstInit}
\StrBehind{#1}{ }[\second]%
\StrRemoveBraces{\second}[\SecondInit]%
\StrChar{\SecondInit}{1}[\SecondInitial]
}
\newcommand*{\makeskeleton}{
\begin{textblock}{20}(0,0)
\ifthenelse{\equal{\cvauthor}{}}{}{
%\Huge\textcolor{darkYellow}{\textbf{\cvauthor}}
% \renewcommand{\name}{\cvauthor}
% \name
\ExtractInitials{\mycvauthor}
\FirstInitial
\SecondInitial
}
\end{textblock}
}

Related

Multiset notation in LaTeX

Does anyone know how to make (nice looking) double bracket multiset notation in LaTeX, i.e something like (\binom{n}{k}) where there are two outer brackets instead of 1 as in binomial? You can see an example of what I mean in http://en.wikipedia.org/wiki/Multiset under the heading "Multiset coefficients" with the double brackets.
In Wikipedia they typeset it as:
\left(\!\!{n\choose k}\!\!\right)
but although this works well for LaTeX in maths mode, with inline equations the outer bracket becomes much larger than the inner bracket.
I have also tried using
\genfrac{((}{))}{0pt}{}{n}{k}
but it has an error with the double brackets.
I am using \binom as well in my document, so I would like the bracket sizes to be similar for \binom and \multiset.
You can explicitly specify the size of the brackets via
\big( \Big( \bigg( or \Bigg(
Then use \! for negative space to get the brackets closer to each other.
One can use the e-TeX \middle command as follows:
\newcommand{\multibinom}[2]{
\left(\!\middle(\genfrac{}{}{0pt}{}{#1}{#2}\middle)\!\right)
}
This assumes that you are using the AMSmath package. If not, replace \genfrac with the appropriate construct using \atop.
(Of course this is a hack: the proper solution would be scalable glyphs for the doubled parenthesis, but I can't find any fonts that provide it.)
I'm surprised it wasn't googlable either, so I'll provide a solution here for posterity's sake.
It is also possible to define two different new commands, using \tbinom and \dbinom (section 4.11.2 of the User's Guide for the amsmath Package):
\documentclass{article}
\usepackage{amsmath}
\newcommand{\inlinebnm}[2]{\ensuremath{\big(\!\tbinom{#1}{#2}\!\big)}}
\newcommand{\displybnm}[2]{\bigg(\!\!\dbinom{#1}{#2}\!\!\bigg)}
\begin{document}
Text $\inlinebnm{a}{b}$ text. %% inline
Text \inlinebnm{a}{b} text. %% inline (also ok thanks to ensuremath)
\[
\displybnm{a}{b} %% display-style
\]
\end{document}

C Source Code in Latex document

Can anyone recommend me a good template to include C source code with line numbering
in Latex? For example, taking the classical Hello world program, I would like to make it look as follows:
(1) /* Hello World program */
(2)
(3) #include<stdio.h>
(4)
(5) main()
(6) {
(7) printf("Hello World");
(8) }
Typicall, I always used the verbatim environment, but I am wondering if there is a better and nicer way to do that.
Thanks so much
Richard
As others have said, the listings package will probably do what you want using something like the following:
\lstset{
language=C, % choose the language of the code
numbers=left, % where to put the line-numbers
stepnumber=1, % the step between two line-numbers.
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=true, % sets if automatic breaks should only happen at whitespace
title=\lstname, % show the filename of files included with \lstinputlisting;
}
\lstinputlisting{HelloWorld.c}
A more powerful alternative would be to use the minted package, although this will do much more than what you're currently asking, as it uses/requires pygments to be installed on your system so that it can fully tokenize the code you give it.
You might want to have a look at the listings package. It is very flexible and easy to use.
Take a look at Code listings in LaTeX. You'll find a couple of alternatives there. Some options are:
listings
minted
lgrind
Use lgrind package for latex. It converts your code into a .tex file
CWEB had a nice C formatter.

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 define output depending on an variable with an status in LaTeX?

I have some standard-texts, but some portion of it is different. But of these different parts only a few exists.
For instance I want:
\mytext{...}{a}
\mytext{...}{b}
That produces:
\section{Item: ...}\label{item...}
This is a standard item. Items of type a are very precious.
\section{Item: ...}\label{item...}
This is a standard item. Items of type b are cheap.
A simple solution to this would be to define commands mytexta and mytextb, but as I have more options I want more something like an if or switch in programming languages. Has anyone a solution for this problem?
The ifthen package (which is included in a standard LaTeX installation) defines a command \ifthenelse, which is used like this:
\usepackage{ifthen}
\ifthenelse{test}{then-code}{else-code}
so you could do something similar to:
\newcommand\mytext[1]{%
\ifthenelse{\equal{#1}{a}}{very precious}{%
\ifthenelse{\equal{#1}{b}}{cheap}{unknown}}}
For LaTeX programming, I'd recommend getting a copy of The LaTeX Companion. It's a really good reference for this stuff.
You can use \newif\iffoo to declare a new condition.
Then \footrue or \foofalse sets it to true or false, and you can use it by \iffoo ... \else ... \fi.
There are more conditionals, see pages 209ff in the TeXbook.
Another option is to use etoolbox (which works with XeLaTeX), below is an MWE
\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\newcommand{\mytext}[1]
{
\ifstrequal{#1}{a} %% make the first comparison
{ %% print text in the first scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type a are very precious.
}
{} %% do nothing if false
\ifstrequal{#1}{b} %% make the second comparison
{ %% print text in the second scenario
\section{Item: #1}\label{item.#1}
This is a standard item. Items of type b are cheap.
}
{} %% do nothing if false
}
\mytext{a}
\mytext{b}
\\
\noindent
We can refer to sections \ref{item.a} and \ref{item.b}
\end{document}
and it produces

Resources