# Get part of a string

I'm new to LaTeX and want to make a journal. I've used this to generate a journal from files in multiple directories.

My main problem is that having various folders named Jan/Feb/...Ago/Dec... makes them look nasty in explorer (alphabetical makes the months show up funny: Ago then Dec then Jan ...) so I prefixed my folders with 01Jan 02Feb ect...

But I don't want the prefixed number to show, so I want to do something like

\substring{01Jan}{3}{5}

Here's what I've got so far and I get the following error: "Use of \myRenamedMonth doesn't match its definition

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.
\usepackage{stringstrings} % \substring{abcdefgh}{3}{6} gives me the 3rd to 6th char = "cdef"

{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2017}
\pgfmathtruncatemacro{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
{   \ifthenelse{\writetitle=1}{#1}{}
}

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}
{   \foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}
{   \foreach \Day in {01,02,03,04,05,06,07,08,09,10,...,31}
{   \IfFileExists{\Year/\Month/\Day}
{   \openin\mysource=\Year/\Month/\Day.tex
\closein\mysource
\xdef\writetitle{1}
\def \myRenamedMonth=\substring{\Month}{3}{6}
\begin{loggentry}{\Year - \myRenamedMonth - \Day}{\firstline}
\xdef\writetitle{0}
\input{\Year/\Month/\Day}
\end{loggentry}
}
{   % files does not exist, so nothing to do

}

}
}
}

\end{document}


EDIT: the problem was solved by using the correct syntax as apointed by Skillmon (please make submit it as an anwser so I can accept it :) but raised another problem... The second dash "-" is getting "eaten up" and printing:

2018 - Jan 03

instead of 2018 - Jan - 03

• \def\myRenamedMonth= isn't correct syntax. It should be \def\myRenamedMonth{\substring{\Month}{3}{6}}. If you always need to remove the first 2 characters out of 5 you could as well use the naive way: \def\RemoveTwoFromFive#1#2#3#4#5\q{#3#4#5} and then \xdef\myRenamedMonth{\expandafter\RemoveTwoFromFive\Month\q}. – Skillmon Mar 18 '18 at 21:06
• Using the correct syntax solved my problem but raised another :P the \begin{loggentry}{\Year - \myRenamedMonth - \Day}{\firstline} line is printing out "2018 - Jan 02", it's eating up the second "-" – Pedro Mar 18 '18 at 21:10
• @Pedro: Actually, it is better to ask the person that gave you the solution in a comment to provide a real answer instead of adding it to the post ... – user31729 Mar 18 '18 at 21:18
• @ChristianHupfer I'm working on it :) – Skillmon Mar 18 '18 at 21:19
• @Skillmon: Have seen it ;-) – user31729 Mar 18 '18 at 22:54

You used the wrong syntax to define \myRenamedMonth. Correct is: \def\myRenamedMonth{\substring{\Month}{3}{6}} (though 01Jan has only 5 characters, so it should be \substring{\Month}{3}{5}).

My proposed naive way doesn't suffer from the removal of the second dash. Also \Year - \myRenamedMonth - \Day eats the spaces in front of the dashes. Correct would be \Year\ - \myRenamedMonth\ - \Day, but perhaps you should use different delimiters (maybe --?), too.

Also you should not put space after the opening brace of your \newcommand and \foreach and stuff. Those do produce a space in your output which is most likely unwanted. Also make sure to put a % after a closing brace in the end of lines if you don't want to put a space there.

The following hopefully does remove all those unwanted spaces. It also does what you want without the issue of removing the dash with my approach (with a few saveguards just to make sure).

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.
%\usepackage{stringstrings} % \substring{abcdefgh}{3}{6} gives me the 3rd to 6th char = "cdef"

{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2017}
\pgfmathtruncatemacro{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
{\ifthenelse{\writetitle=1}{#1}{}}

\makeatletter
\long\def\removeFirstTwo#1%
{%
\if\relax\detokenize{#1}\relax%just to make sure the argument isn't empty
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\expandafter\removeFirstTwo@i#1\q}%
}
\long\def\removeFirstTwo@i#1#2#3\q{#3}
\makeatother

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}
{\foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}
{\foreach \Day in {01,02,03,04,05,...,31}
{\IfFileExists{\Year/\Month/\Day}
{%
\openin\mysource=\Year/\Month/\Day.tex
\closein\mysource
\def\writetitle{1}% no need for xdef here
\xdef\myRenamedMonth{\removeFirstTwo{\Month}}%
\begin{loggentry}{\Year\ - \myRenamedMonth\ - \Day}
{\firstline}%
\def\writetitle{0}% no need for xdef here
\input{\Year/\Month/\Day}
\end{loggentry}%
}
{% files does not exist, so nothing to do
}
}%
}%
}%
\end{document}


EDIT: You might also want to change your \mytitle to something like:

\newcommand{\mytitle}
{
\ifnum\writetitle=1
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
}


Or change to use boolean values (\newif\ifwritetitle and then \writetitletrue and \writetitlefalse to flip them, and instead of \ifnum\writetitle=1 just use \ifwritetitle).

No need for \ifthenelse here.

• This seems overly complicated. \def\gobbletwo#1#2{} along with \edef\name{\expandafter\gobbletwo\Month} seems sufficient. – TH. Mar 18 '18 at 21:41
• @TH. if the names used in the argument do indeed match the pattern you're right. If not \gobbletwo might fail badly. Also \gobbletwo should be \long. – Skillmon Mar 18 '18 at 21:43
• I can't think of any reason for it to be \long in this case. You know exactly what the arguments to it are going to be. – TH. Mar 18 '18 at 21:54
• @TH. If you say so. But since \@gobbletwo is long I think you should either use \@gobbletwo or define \gobbletwo to be long, too. All my code does is solving the question while being "overly complicated" in order to be less error prone if the input is not as ideally defined as it is now. The community will vote @egreg's answer higher than mine and OP will see that it's the way more people would choose. I would, too, just for myself and it is perfectly fine if everybody does, as long as they keep the restrictions in mind. – Skillmon Mar 18 '18 at 22:02
• using \@gobbletwo definitely seems like the way to go. I just didn't know it existed. – TH. Mar 19 '18 at 1:24

The line

\def \myRenamedMonth=\substring{\Month}{3}{6}


is completely wrong, I'm afraid.

\makeatletter
\newcommand{\getRenamedMonth}{%
\edef\myRenamedMonth{\expandafter\@gobbletwo\Month}%
}
\makeatother


in the preamble and use \getRenamedMonth instead of that faulty line.

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.

{\par\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}% what for?

\newcommand{\StartYear}{2017}
\newcommand{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]{%
\ifthenelse{\writetitle=1}{#1}{}%
}

\makeatletter
\newcommand{\getRenamedMonth}{%
\edef\myRenamedMonth{\expandafter\@gobbletwo\Month}%
}
\makeatother

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}{%
\foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}{%
\foreach \Day in {01,02,03,04,05,06,07,08,09,10,...,31}{%
\IfFileExists{\Year/\Month/\Day}
{\openin\mysource=\Year/\Month/\Day.tex
\closein\mysource
\gdef\writetitle{1}%
\getRenamedMonth
\begin{loggentry}{\Year - \myRenamedMonth - \Day}{\firstline}
\gdef\writetitle{0}
\input{\Year/\Month/\Day}
\end{loggentry}%
}
{}% IfFileExists
}%
}%
}

\end{document}