# How to draw a text box in a map?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339>\
**Category:** GMT.jl Q&A\
**Created:** [November 21, 2021, 8:25pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339 "2021-11-21T20:25:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon6j](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@leon6j](https://forum.generic-mapping-tools.org/u/leon6j)\
**Post date:** [November 21, 2021, 8:25pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/1 "2021-11-21T20:25:42Z")

</div>

My goal is to plot a text box like the below onto my map:  
A - Name1  
B - Name2  
C - Name3  
…

I stored the above info in a text file named “text.rtf”, and below is the code to draw the text box:  
`text!("/Path/text.rtf", font="5p,Times-Bold,black", x=195, y=45);`

Unfortunately, I got a ton of errors like the below. I wonder if anyone could tell me what I did wrong? Many thanks!

```
pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 1 in file /Path/text.rtf
pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 5 in file /Path/text.rtf
pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 6 in file /Path/text.rtf
Warning: the following options were not consumed in pstext => [:y, :x]
psxy [WARNING]: 2 external clip operations were not terminated!
Error: /syntaxerror in /----nostringval----
Operand stack:

Execution stack:
   %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1990 1 3 %oparray_pop 1989 1 3 %oparray_pop 1977 1 3 %oparray_pop 1833 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push
Dictionary stack:
   --dict:768/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)-- --dict:162/250(L)--
Current allocation mode is local
psconvert [ERROR]: System call [gs -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true '/var/folders/28/fc899mm50s3dn8f971z3vbwm0000gn/T/GMTjl_tmp.ps' 2> './psconvert_30410c.bb'] returned error 256.
ERROR: LoadError: Something went wrong when calling the module. GMT error number = 79
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] gmt(::String)
   @ GMT ~/.julia/packages/GMT/TPV4a/src/gmt_main.jl:288
 [3] showfig(d::Dict{Symbol, Any}, fname_ps::String, fname_ext::String, opt_T::String, K::Bool, fname::String)
   @ GMT ~/.julia/packages/GMT/TPV4a/src/common_options.jl:3273
 [4] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool, ::Vector{GMTdataset}, ::Vararg{Any})
   @ GMT ~/.julia/packages/GMT/TPV4a/src/common_options.jl:3428
 [5] common_plot_xyz(::String, ::Vector{GMTdataset}, ::String, ::Bool, ::Bool, ::Pair{Symbol, Any}, ::Vararg{Pair{Symbol, Any}})
   @ GMT ~/.julia/packages/GMT/TPV4a/src/psxy.jl:215
 [6] #plot#296
   @ ~/.julia/packages/GMT/TPV4a/src/plot.jl:112 [inlined]
 [7] #plot!#297
   @ ~/.julia/packages/GMT/TPV4a/src/plot.jl:114 [inlined]
 [8] top-level scope
```

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [November 22, 2021, 6:32pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/2 "2021-11-22T18:32:59Z")

</div>

Really? Are you really expecting that GMT can read the Microsoft rtf format?

But your command wouldn’t work anyway even if the file was in plain ASCII. Maybe you are after the `plot(...., legend=(label="BlaBla",))` option. Otherwise you must read the multi-paragraph mode of `text` (probably must check the GMT `text` manual).

---

<div class="post-metadata">

**Author:** ![tonino13](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/tonino13/32/1440_2.png) [@tonino13](https://forum.generic-mapping-tools.org/u/tonino13)\
**Post date:** [November 23, 2021, 12:58am UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/3 "2021-11-23T00:58:37Z")

</div>

@leon6j, try this input file for legend.  
[legend\_example\_input.dat](https://forum.generic-mapping-tools.org/uploads/short-url/sJlSMxB9AiCqjHRLq0Ljcd9xmQh.dat) (289 Bytes)

Then use:

> fig.legend(spec=r’Path\_to\_file\legend\_example\_input.dat’,  
> position=“jTR+o0.3c+w5.5c”, box=True)

Follow also @Truyenpt suggestions.  
Stay safe.  
Tonino

---

<div class="post-metadata">

**Author:** ![leon6j](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@leon6j](https://forum.generic-mapping-tools.org/u/leon6j)\
**Post date:** [November 24, 2021, 4:26pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/4 "2021-11-24T16:26:20Z")

</div>

Thank you so much! 👍This works.

I had to make some small changes for Julia:  
`legend!("legend_input.dat", position="jTR+o0.3c+w5.5c", box=1);`

Would you please help me with another question? How do I specify the font size and line spacing of my legend?

Many thanks.

---

<div class="post-metadata">

**Author:** ![tonino13](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/tonino13/32/1440_2.png) [@tonino13](https://forum.generic-mapping-tools.org/u/tonino13)\
**Post date:** [November 25, 2021, 7:06pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/5 "2021-11-25T19:06:01Z")

</div>

Dear @leon6j,  
To avoid confusing you I send you the following link:  
[GMT\_legend](https://docs.generic-mapping-tools.org/dev/legend.html)

You should increase / decrease the number after the “G” letter (on the input file) for the space between lines

For the font, you can try changing the number 0.7 ( “0.7c El-Alto”)  
However you may see the example at the end of the link I sent, I hope this answer your question.  
Stay safe and best regards  
Tonino

---

<div class="post-metadata">

**Author:** ![leon6j](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@leon6j](https://forum.generic-mapping-tools.org/u/leon6j)\
**Post date:** [November 25, 2021, 8:09pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/6 "2021-11-25T20:09:24Z")

</div>

Thank you for the reply. Based on the menu, the number before the text is the distance from the left boundary of the legend.

The manual says to use `FONT_ANNOT_PRIMARY` to control and font and size. I put the below before the text in the dat file, but it did not work. GMT simply show them as if they were intended to be part of the text.  
`FONT_ANNOT_PRIMARY = [12p,Helvetica,black]`

---

<div class="post-metadata">

**Author:** ![tonino13](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/tonino13/32/1440_2.png) [@tonino13](https://forum.generic-mapping-tools.org/u/tonino13)\
**Post date:** [November 26, 2021, 12:03am UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/7 "2021-11-26T00:03:15Z")

</div>

Dear @leon6j,  
Maybe this link can help you:  
[GMT\_Set\_Defaults\_Parameters](https://flint.soest.hawaii.edu/t/how-to-set-default-parameters-in-pygmt/240/9)  
Hope this help, stay safe and best regards.  
Tonino

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [November 26, 2021, 3:31pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/8 "2021-11-26T15:31:34Z")

</div>

> [@leon6j](#):
>
> FONT\_ANNOT\_PRIMARY = [12p,Helvetica,black]

Since when can we do this? There are over 150 parameters we can configure and there is obvious no checking for them individually in the wrapper. The syntax, as it appears in several examples nd in the docs, is `par=(FONT_ANNOT_PRIMARY = "12p,Helvetica,black",)`

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [November 26, 2021, 3:32pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/9 "2021-11-26T15:32:51Z")

</div>

Tonino, this not pygmt but GMT.jl 🙂

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [November 26, 2021, 9:01pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/10 "2021-11-26T21:01:25Z")

</div>

`pslegend` is not the friendliest of the modules at the moment (hard to _verbalize_ all those codes) but it doesn’t take much to reproduce the example in the man page (GMT man page).

```auto
makecpt("-Cpanoply -T-8/8 > tt.cpt")

T = text_record(["G -0.1i",
       "H 24p,Times-Roman My Map Legend",
       "D 0.2i 1p",
       "N 2",
       "V 0 1p",
       "S 0.1i c 0.15i p300/12 0.25p 0.3i This circle is hachured",
       "S 0.1i e 0.15i yellow 0.25p 0.3i This ellipse is yellow",
       "S 0.1i w 0.15i green 0.25p 0.3i This wedge is green",
       "S 0.1i f 0.25i blue 0.25p 0.3i This is a fault",
       "S 0.1i - 0.15i - 0.25p,- 0.3i A contour",
       "S 0.1i v 0.25i magenta 0.5p 0.3i This is a vector",
       "S 0.1i i 0.15i cyan 0.25p 0.3i This triangle is boring",
       "D 0.2i 1p",
       "V 0 1p",
       "N 1",
       "M 5 5 600+u+f",
       "G 0.05i",
       "I @SOEST_block4.png 3i CT",
       "G 0.05i",
       "B tt.cpt 0.2i 0.2i -B0",
       "G 0.05i",
       "L 9p,Times-Roman R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000",
       "G 0.1i",
       "T Let us just try some simple text that can go on a few lines.",
       "T There is no easy way to predetermine how many lines may be required",
       "T so we may have to adjust the height to get the right size box."]);

legend(T, region=(0,10,0,10), pos=(paper=true, anchor=(1.25,1.25), width=14,
            justify=:BL, spacing=1.2), clearance=(0.25,0.25), box=(pen=0.5, fill=:azure1),
           show=true, proj=:Mercator)

```

Note, one could also have used the more cryptic

```auto
R="0/10/0/10", J="M6i", D="x0.5i/0.5i+w5i+jBL+l1.2", C="0.1i/0.1i", F="+p+gazure1+r", ...

```

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/d/d1c1484612976d208e4dd05657c3085a507b059b.png)

---

<div class="post-metadata">

**Author:** ![leon6j](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@leon6j](https://forum.generic-mapping-tools.org/u/leon6j)\
**Post date:** [November 26, 2021, 11:54pm UTC](https://forum.generic-mapping-tools.org/t/how-to-draw-a-text-box-in-a-map/2339/11 "2021-11-26T23:54:51Z")

</div>

Thanks, Joaquim.

It seems that the font size is by default in this example?
