# Print a term with variables as letters, not numbers

**URL:** <https://swi-prolog.discourse.group/t/print-a-term-with-variables-as-letters-not-numbers/1803>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [January 30, 2020, 12:36am UTC](https://swi-prolog.discourse.group/t/print-a-term-with-variables-as-letters-not-numbers/1803 "2020-01-30T00:36:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kowalsky](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kowalsky/32/394_2.png) [@kowalsky](https://swi-prolog.discourse.group/u/kowalsky)\
**Post date:** [January 30, 2020, 12:36am UTC](https://swi-prolog.discourse.group/t/print-a-term-with-variables-as-letters-not-numbers/1803/1 "2020-01-30T00:36:30Z")

</div>

I want to print a term with variable letters like this:

```prolog
?- print_term(f(X,Y,X), []).
f(A,_,A)

```

I want to do the similar thing with `format/2` or `3` and I cannot find correct options for it.

```prolog
?- format('~W', [f(X,Y,X), [numbervars(false)]]).
f(_12036,_12038,_12036)
?- format('~W', [f(X,Y,X), [numbervars(true)]]).
f(_12036,_12038,_12036)
?- format('~W', [f(X,Y,X), []]).
f(_12036,_12038,_12036)

```

I am using swipl 8.0.3

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 30, 2020, 7:45am UTC](https://swi-prolog.discourse.group/t/print-a-term-with-variables-as-letters-not-numbers/1803/2 "2020-01-30T07:45:21Z")

</div>

Use this. Note that the `numbersvars(true)` option of the write family just tell these predicates to interpret `'$VAR'(N)` as A, B, … You have to number the variables yourself. Note that if you need to print multiple terms with possible sharing variables you should numbervars them together, for example by passing a list to numbervars/4.

Finally, the double negation is to make sure that the bindings are undone before proceeding and thus the term is not modified by the operation.

```prolog
    \+ \+ ( numbervars(Term, 0, _, [singletons(true)]),
            format('~W', [Term, [numbervars(true)]])
          )

```

---

<div class="post-metadata">

**Author:** ![kowalsky](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kowalsky/32/394_2.png) [@kowalsky](https://swi-prolog.discourse.group/u/kowalsky)\
**Post date:** [January 30, 2020, 11:29am UTC](https://swi-prolog.discourse.group/t/print-a-term-with-variables-as-letters-not-numbers/1803/3 "2020-01-30T11:29:25Z")

</div>

> [@jan](#):
>
> + + ( numbervars(Term, 0, \_, [singletons(true)]), format(‘~W’, [Term, [numbervars(true)]]) )

Thank you for your help and explanation.

A note, `format` uses `numbervars(true)` option by default. So only `format('~w', [Term])` also works with `numbervars`.  
`singletons(true)` I like this option to reveal singleton vars.

```prolog

```
