# Can I convert a string to catcode 11?

In the TeX FAQ there's a way to test if the jobname is equal to something (comparing the jobname). The difficulty of the naïve test is that the contents of the \jobname primitive are catcode 12 so to test if the \jobname is, say, myfile we need to create a macro containing the string myfile but with catcode 12 and use that to compare against \jobname.

This is all very well if you control the test. But what if you don't? What if there's some test deep in some package that you want to pass the \jobname (or some part of it) to and this test is against a predefined word of catcodes 11.

Is there some way, like \meaning, that can convert its contents to catcode 11?

Added in edit: An example of possible use has been called for.

\documentclass{beamer}
%\url{http://tex.stackexchange.com/q/117892/86}

\expandafter\includeonlylecture\expandafter{\jobname}

\begin{document}

\lecture{An Astonishing Result}{euclid}

\begin{frame}{Euclid's Astonishing Result}
\end{frame}

\lecture{A Boring Result}{pythagoras}

\begin{frame}{Pythagoras' Boring Result}
\end{frame}

\end{document}


The intention being that:

pdflatex -jobname euclide lectures.tex


and

pdflatex -jobname pythagoras lectures.tex


produce different lectures.

• Jun 6 '13 at 10:10
• Are we allowed to use e-TeX? What do we do about non-letter characters? Jun 6 '13 at 10:11
• e-TeX's \scantokens could be used or each character could be replaced by its letter counterpart. But the trouble with special characters (underscore, @, ...) remains. It is much more efficient and robuster to convert the other string to catcode 12 characters before the comparison. Jun 6 '13 at 10:15
• Joseph: Yes, e-TeX is allowed. As for non-letter characters, let's assume that I'm in a very controlled environment with regards to what I'm testing and can guarantee that \jobname (or an extracted part of it) consists only of letters. Heiko: Granted, but consider this more of a "If I really have to do this, what do I do?". Jun 6 '13 at 10:21
• @AndrewStacey Probably you should ask the maintainer of beamer to allow for string-based comparisons here :-) Jun 6 '13 at 15:10

I don't understand very well the question; category code 12 is used exactly for avoiding problems with category codes and special characters.

With \scantokens you can do it: save the following code as ncjob.tex and run it.

\documentclass{article}

\begingroup
\everyeof{\noexpand}
\endlinechar=-1
\xdef\normaljobname{\scantokens\expandafter{\jobname}}
\endgroup

\def\test{ncjob}

\ifx\test\normaljobname\typeout{SUCCESS}\else\typeout{FAILURE}\fi

\stop


SUCCESS

Important note This requires that the jobname contains only “safe characters”; something like ~ in the file name would make this die horribly.

A possibly safer way is to use expl3:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }
\NewDocumentCommand{\definenormaljobname}{O{\normaljobname}}
{
\tl_set_rescan:NnV #1 { } \c_job_name_tl
}
\ExplSyntaxOff
\definenormaljobname
\def\test{nc~job}\show\normaljobname

\ifx\test\normaljobname\typeout{SUCCESS}\else\typeout{FAILURE}\fi

\stop


Tested with the command line

pdflatex -jobname 'nc~job' ncjob


This is how your example could be managed; don't use special characters or spaces in the jobname passed from the command line (nor in the lecture labels, of course).

\documentclass{beamer}

\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }
\NewDocumentCommand{\includelecturefromjobname}{}
{
\tl_set_rescan:NnV \l_tmpa_tl { } \c_job_name_tl
\exp_args:NV \includeonlylecture \l_tmpa_tl
}
\ExplSyntaxOff

\includelecturefromjobname

\begin{document}

\lecture{An Astonishing Result}{euclid}

\begin{frame}{Euclid's Astonishing Result}
\end{frame}

\lecture{A Boring Result}{pythagoras}

\begin{frame}{Pythagoras' Boring Result}
\end{frame}


## Note

As of September 2015, instead of \c_job_name_tl one should use

\c_sys_jobname_str

• This is asking for trouble, if \jobname contains special characters, e.g. with active catcodes, ... Jun 6 '13 at 10:16
• @HeikoOberdiek Hence my comment on the question: what are we expected to do in these cases :-) Jun 6 '13 at 10:17
• @JosephWright: Sorry, missed that. :-( Jun 6 '13 at 10:20
• I'd tried a few things with \scantokens before asking but none of them worked. Care to explain why the \everyeof{\noexpand}\endlinechar=-1 are there? Jun 6 '13 at 10:22
• @HeikoOberdiek I know; I added a caveat. Jun 6 '13 at 10:22

This allows the filename to have special characters, and still do the comparison (in this case, everything is converted to catcode 12 for the comparison)

\documentclass{article}
\usepackage{ifthen}
\def\display#1{\detokenize\expandafter{#1}}
\def\comparenames#1{%
\ifthenelse{\equal{\display{#1}}{\display{\comparetest}}}{T}{F}}
\begin{document}
\def\comparetest{myfile}

\def\jbnm{myfile}
\comparenames{\jbnm}

\def\jbnm{\\$&o)-}
\comparenames{\jbnm}

\end{document}


Save this file as cxviiviiicxcii.tex:

\documentclass{article}

% Save this file as cxviiviiicxcii.tex

\begin{document}

\def\alltoletter {%
\count 255 0
\loop
\catcode\count255 11
\ifnum\count 255 < 255 \advance \count 255 1 \repeat }

\def\convert #1\to #2{%
\begingroup
\everyeof{\noexpand}%
\endlinechar -1
\alltoletter
\edef\x{\scantokens\expandafter{#1}}%
% earlier silly stuff (sorry):  \expandafter\expandafter\expandafter\endgroup
\expandafter\endgroup
\expandafter\def\expandafter #2\expandafter{\x}%
}%

\def\myname {cxviiviiicxcii}%

\convert\jobname\to\jobnameconverted

converted: \ifx\myname\jobnameconverted EQUAL\else UNEQUAL\fi

unconverted: \ifx\myname\jobname EQUAL\else UNEQUAL\fi

\begingroup
\escapechar -1
\xdef\testtwoA {\string\abcdef}
\endgroup

\convert\testtwoA\to\testtwoAconverted

\def\testtwoB {abcdef}

converted: \ifx\testtwoB\testtwoAconverted EQUAL\else UNEQUAL\fi

unconverted: \ifx\testtwoB\testtwoA EQUAL\else UNEQUAL\fi
\end{document}


• A comment: if the macro whose contents are to be converted to catcode 11 contains a control sequence, say \abc then it seems that \scantokens will transform it into \abc  where the first four have catcode 11, and the space is also of catcode 11; except if this \abc is the last thing just before the closing brace in the initial definition of the to-be-converted macro (contents), then no space. So the \alltoletter should possibly end in \catcode32 10, in order for the spaces produced by \scantokens to be standard, thus easing up use of the \convert thing.
– user4686
Jun 6 '13 at 17:42