Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to allow the user to interact with the document at compilation time, for example to choose the color of hyperlinks.

For that, I created a new if and used \typein like in the following example :

\documentclass{article}
\usepackage[colorlinks]{hyperref} 
\newif\ifcllinks
\newcommand\cllink{}
\typein[\cllink]{Coloried links (true or false) ?}
\csname cllinks\cllink \endcsname
\ifcllinks
    \hypersetup{linkcolor=red,citecolor=blue,filecolor=green,urlcolor=blue}
\else
    \hypersetup{linkcolor=black,citecolor=black,filecolor=black,urlcolor=black}
\fi
\begin{document}
\section{Title}\label{one}
The section \ref{one}
\end{document}

This works, but I'm not satisfied because of the supplementary macro \cllink that isn't very necessary (I think).

I try

\newif\ifcllinks
\csname cllinks\typein{Coloried links (true or false) ?} \endcsname

But this doesn't work (missing \endcsname inserted).

Is it possible to make it works with some TeX hackery (\expandafter, \string, …) ?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

No it isn't possible to make this work.

The content of \csname ... \endcsname must be fully expandable, otherwise an error is raised. However \typein makes an assignment and assignments are not expandable, but must be executed. No TeX hackery can change this.

So

\typein[\cllink]{Coloried links (true or false) ?}
\csname cllinks\cllink \endcsname

it as small as it gets.

You could make the macro definition local if you don't want it global:

\begingroup
\typein[\cllink]{Coloried links (true or false) ?}
\expandafter
\endgroup
\csname cllinks\cllink \endcsname

but the code wont be any shorter.

share|improve this answer
    
I'm disappointed… but however it's a nice answer. –  Matsaya Mar 3 '11 at 22:12
add comment

One answer is already accepted, but I am going to throw mine out there anyhow: Why insist that the user type “true” or “false”? Here is a variant that will accept any answer starting with a lower case “y” as an affirmative, anything else meaning no:

\documentclass{minimal}
\makeatletter
\def\ask#1{\typeout{#1}%
  \begingroup \endlinechar-1 \read-1 to\answer
  \expandafter\futurelet\expandafter\answer\expandafter\@ask\answer.\@nil}
\def\@ask#1\@nil
  {\ifx y\answer\expandafter\@ask@y\else\expandafter\@ask@n\fi}
\def\@ask@y#1#2{\endgroup#1}
\def\@ask@n#1#2{\endgroup#2}
\makeatother
% demo:
\ask{Answer yes or no:}{\message{[yes]}}{\message{[no]}}
\ask{And again:}{\message{[yes]}}{\message{[no]}}
\begin{document}
\end{document}

The definition is a bit longer, but here is a general mechanism for asking a yes-or-no question. For your application, you could use it as

\ask{colored links?}
  {\hypersetup{linkcolor=red,citecolor=blue,filecolor=green,urlcolor=blue}}
  {\hypersetup{linkcolor=black,citecolor=black,filecolor=black,urlcolor=black}}
share|improve this answer
    
I have not even think to that, but it's a good idea –  Matsaya Mar 3 '11 at 23:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.