GeneralCan the result of a command be stored in a variable?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
alanq
Posts: 21
Joined: Mon Dec 28, 2009 3:03 am

Can the result of a command be stored in a variable?

Post by alanq »

Is there a way in LaTeX to store the result of a command in a variable, rather than the command itself?

I'm using \dayofweekname from the datetime package.
Given \dayofweekname{2011}{03}{22} the result is 'Tuesday'.
This works fine on normal lines, but results in an error in section titles.

Min. Working Example

Code: Select all

\documentclass{article}

\usepackage{datetime}

\begin{document}

\dayofweekname{22}{03}{2011}

\section{Date}
\section{Date \dayofweekname{22}{03}{2011} }

\end{document}
...which results in:

Code: Select all

This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
(./ConstantsProblem.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, ukenglish, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo))
(/usr/share/texmf-texlive/tex/latex/datetime/datetime.sty
(/usr/share/texmf-texlive/tex/latex/fmtcount/fmtcount.sty
(/usr/share/texmf-texlive/tex/latex/base/ifthen.sty)
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/latex/amsmath/amsgen.sty)
(/usr/share/texmf-texlive/tex/latex/fmtcount/fc-english.def)
(/usr/share/texmf-texlive/tex/latex/fmtcount/fc-UKenglish.def)
(/usr/share/texmf-texlive/tex/latex/fmtcount/fc-british.def)
(/usr/share/texmf-texlive/tex/latex/fmtcount/fc-USenglish.def)
No configuration file fmtcount.cfg found.
)) (./ConstantsProblem.aux))
! Incomplete \iffalse; all text was ignored after line 10.
<inserted text> 
                \fi 
<*> ConstantsProblem.tex
                        
? 
So, a rewording of my question could be:
Can the result of \dayofweekname{22}{03}{2011}, ie 'Tuesday', be stored in a variable using something like \let or \def or ... ?

Any ideas gratefully received.
Thanks
Last edited by alanq on Fri May 06, 2011 11:54 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Can the result of a command be stored in a variable?

Post by cgnieder »

Hi,

you need to "protect" \dayofweekname:

Code: Select all

\documentclass{article}

\usepackage{datetime}

\begin{document}

\dayofweekname{22}{03}{2011}

\section{Date}
\section{Date \protect\dayofweekname{22}{03}{2011} }

\end{document}
Regards
site moderator & package author
alanq
Posts: 21
Joined: Mon Dec 28, 2009 3:03 am

Re: Can the result of a command be stored in a variable?

Post by alanq »

Brilliant!
Thank you, cgnieder.
My immediate problem is solved.

The question now becomes,

a) does protection negate the need for a way of putting the result of a command into a constant/variable?

b) is there a way to do that anyway?

Cheers
Alan
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Can the result of a command be stored in a variable?

Post by cgnieder »

I'm not sure, I understand what you mean.

The reason, your first attempt failed, was that \dayofweekname is a fragile command. And because \section has a moving argument (which is written into the TOC, for example), it needed to be made robust. That's what the \protect was for.
You might want to read http://www.tex.ac.uk/cgi-bin/texfaq2html?label=protect.

You have a number of ways to solve your problem:

Code: Select all

\documentclass{article}

\usepackage{datetime}

\begin{document}

\dayofweekname{22}{03}{2011}

\section{Date}
% direct protection:
\section{Date \protect\dayofweekname{22}{03}{2011} }

% save specific date:
\newcommand*\thisDayofweekname{\protect\dayofweekname{22}{03}{2011}}
\section{Date \thisDayofweekname}

% robust command #1:
\newcommand*\myDayofweekname{\protect\dayofweekname}
\section{Date \myDayofweekname{22}{03}{2011}}

% robust command #2:
\DeclareRobustCommand*\MyDayofweekname{\dayofweekname}
\section{Date \MyDayofweekname{22}{03}{2011}}

\end{document}
Regards,
Clemens
site moderator & package author
alanq
Posts: 21
Joined: Mon Dec 28, 2009 3:03 am

Can the result of a command be stored in a variable?

Post by alanq »

Thank you, Clemens
Sorry for the delay.

I particularly like the idea of protecting the command in the newcommand declaration.

Cheers
Alan
Last edited by alanq on Sat May 07, 2011 12:55 am, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: Can the result of a command be stored in a variable?

Post by Stefan Kottwitz »

Hi Alan,

in general \edef can be used to store a result in a macro, or its global version \xdef.

Stefan
LaTeX.org admin
alanq
Posts: 21
Joined: Mon Dec 28, 2009 3:03 am

Re: Can the result of a command be stored in a variable?

Post by alanq »

Thank you, Stefan, I hadn't discovered the \def commands before.
You've also now lead me to things like \noexpand.
I'm learning... :)
Post Reply