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.

The problem is to get

enter image description here

The question is justified. The answer is right flush. The dimensions of the rectangle are 5cm and 4cm. The length of the question is unknown.

Manually

\documentclass[11pt]{scrartcl}
\usepackage{tikz}

\begin{document}

  \def\quest{1/(4.2+5.4)-(3.5+4.7)\\/9} 
  \def\ans{-0.806944444} 

\begin{tikzpicture}[font=\Large] 
 \draw (0,0) rectangle (5,-4);
\node[align=justify,text width =5cm-1ex,
      anchor=north west,inner sep=1pt] (qu) {\quest};
\node[align=right,text width =5cm-1ex,
      anchor=north west,inner sep=1pt] (an) at (qu.south west) {\ans}; 
\end{tikzpicture}

\end{document}

and I get

enter image description here

But how to get this automatically ?

Update-information

The same question was asked on the list pgf/tikz. Mark Wibrow (who worked on pgfmath, and other parts of pgf) gave a beautiful solution :

\documentclass{article}
\usepackage{tikz}
\begin{document}

\def\showsum#1{\ifx#1=\par\hfill\else#1\hskip0.1ex 
minus0.5ex\expandafter\showsum\fi}
\tikzset{calculator screen/.style={
    inner sep=2pt,
    fill=blue!20,
    font=\huge\tt,
    execute at begin node={\parindent=0pt\hsize=5cm\vbox to4cm\bgroup},
    execute at end node=\vfill\egroup
}}

\tikz\node[calculator screen]
    {\showsum 1/(4.2+5.4)-(3.5+4.7)/9 +3+2+1= -.80694444};

\end{document} 

Very impressive

share|improve this question

1 Answer 1

up vote 4 down vote accepted

I'd simply add a zero skip between any two tokens and set ragged right the question:

\documentclass{article}
\usepackage{tikz,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\calcinput}{m}
 {
  \tl_map_inline:nn { #1 } { ##1 \hspace{0pt} }
 }
\ExplSyntaxOff



\begin{document}

  \def\quest{\calcinput{1/(4.2+5.4)-(3.5+4.7)/9}}
  \def\ans{\calcinput{-0.806944444}}

\begin{tikzpicture}[font=\Large\ttfamily]
 \draw (0,0) rectangle (5,-4);
\node[align=flush left,text width =5cm,
      anchor=north west,inner sep=1pt] (qu) {\quest};
\node[align=right,text width =5cm,
      anchor=north west,inner sep=1pt] (an) at (qu.south west) {\ans};
\end{tikzpicture}

\begin{tikzpicture}[font=\Large\ttfamily]
 \draw (0,0) rectangle (4,-4);
\node[align=flush left,text width =4cm,
      anchor=north west,inner sep=1pt] (qu) {\quest};
\node[align=right,text width =4cm,
      anchor=north west,inner sep=1pt] (an) at (qu.south west) {\ans};
\end{tikzpicture}

\end{document}

The window width should be an integer multiple of .5em in the monospaced font, in my opinion.

share|improve this answer
    
Fine! but "I'd simply add a zero skip between any two tokens" is not easy for me. I'm not a great TeX(pert) and I know nothing about xparse. Perhaps I may stop working TikZ only to discover a new world alter mundus :) –  Alain Matthes Jun 10 '12 at 9:12
    
@Altermundus I simply process the input adding \hspace{0pt} after each token; so \calcinput{1+2} becomes 1\hspace{0pt}+\hspace{0pt}+2\hspace{0pt} when fed to the text processing done by \node. There are various "old-world" LaTeX ways to do it: with expl3 it's embarassingly easy. :) –  egreg Jun 10 '12 at 9:16
    
Yes I see. The code is very light. Very impressive –  Alain Matthes Jun 10 '12 at 9:26

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.