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.

Deeply inspired by Paulo Cereda's answer on the ways TeX can be used apart from document creation. If you haven't seen the program mentioned: it's a Reversi TeX game that during the compile asks the user for the next move in the game and provides feedback. At the end the whole game is produced as a dvi.

Immediately practical uses of this popped up: Asking users for layout, colors, language. So on to terra incognita!

I tried to dissect the Reversi game, but it's not written for readability. I searched around, but user input to TeX programs is not an everyday thing.

How do I take input from the user compiling the document?

For example, I could ask a user for their name, and then print "Hello, [user's name]".

Update 1: I've now stumbled upon the \read macro, and it seems to be on the way, but it isn't really what I want.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\begin{document}

Hello, \read

\end{document}

Update 2: David Carlisle pointed me to makebst a program to generate bibTeX styles. I'm looking into the source.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

LaTeX provides the command \typein[var]{message} that should do what you're looking for:

\documentclass{article}
\begin{document}
\typein[\name]{What's your name? } % take user input
Hello \name!
\end{document}

There's also the sibling command \typeout{message} to output text on the console.

share|improve this answer
add comment

From makebst I found this code that seems to work in TeX.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\newread\ttyin

\def\defpar{\par}
\def\remblk#1 @@{#1}
\def\ask#1#2{\mes{#2}\read\ttyin to #1\ifx#1\defpar\def#1{}\else
  \edef#1{\expandafter\remblk#1@@}\fi}

\begin{document}

\ask{\name}{What is your name?}

Hello, \name

\end{document}
share|improve this answer
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.