source: trunk/Textoidl/translate_sub_super.pro @ 69

Last change on this file since 69 was 47, checked in by pinsard, 18 years ago

upgrade of TEXT2IDL/TEXtoIDL according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1;  NOTE to future maintainers:
2;   Make sure sub_sup_idl stays before translate_sub_super.  At least
3;   for now, when IDL encounters a function and automatically compiles
4;   it, it only compiles the functions in the file up to the named
5;   function.  So even if sub_sup_idl was declared with
6;   FORWARD_FUNCTION in translate_sub_super, it would not properly
7;   compile.
8;
9;+
10; SPECIAL NOTE:
11;       The file translate_sub_super.pro contains two functions,
12;       translate_sub_super, and sub_sup_idl.  The former is the
13;       generic routine for processing TeX sub/superscripts, the
14;       latter is used only by translate_sub_super and has no general
15;       utility.  Hence it lives here.  You will see documentation for
16;       translate_sub_super second if you use DOC_LIBRARY.
17;-
18;
19;
20;+
21; NAME:
22;       SUB_SUP_IDL
23; PURPOSE:
24;       Return the proper IDL font positioning command for TeX
25;       sub/superscripts.
26; CATEGORY:
27;       TeXtoIDL
28; CALLING SEQUENCE:
29;       fnt = sub_sup_idl( strn )
30; INPUTS:
31;       strn      -- Either '^' or '_', the TeX super/subscript       in
32;                    characters
33; KEYWORD PARAMETERS:
34;       /FORCE_UD -- Set this to use !U/!D instead of !E/!I for
35;                    sub/superscripts .
36;       /HELP     -- Set to print useful message and exit.
37; OUTPUTS:
38;       fnt       -- Either '!U' or !E' for superscripts,             out
39;                    or '!D' or '!I' for subscripts.
40; COMMON BLOCKS:
41; SIDE EFFECTS:
42; NOTES:
43;       Used only by translate_sub_super.  Should be kept in same
44;       file.
45; EXAMPLE:
46; MODIFICATION HISTORY:
47;       $Id$
48;       $Log: translate_sub_super.pro,v $
49;       Revision 1.5  2000/06/14 19:09:22  mcraig
50;       Changed name of strtok str_token to avoid conflict in IDL 5.3.
51;
52;       Revision 1.4  1996/06/14 20:00:27  mcraig
53;       Updated Copyright info.
54;
55;       Revision 1.3  1996/05/09 00:22:17  mcraig
56;       Changed some function calls to reflect changes in those functions, moved
57;       some code out of the main loop that didn't need to be there, added
58;       documentation.
59;
60;       Revision 1.1  1996/01/31 18:47:37  mcraig
61;       Initial revision
62;
63; RELEASE:
64;       $Name: Rel_2_1_2 $
65; COPYRIGHT:
66;  Copyright (C) 1996 The Regents of the University of California, All
67;  Rights Reserved.  Written by Matthew W. Craig.
68;  See the file COPYRIGHT for restrictions on distrubting this code.
69;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
70;-
71FUNCTION Sub_sup_idl, token,  FORCE_UD = force_ud
72
73; provide help if needed.
74    IF (n_params() NE 1) OR keyword_set(Help) THEN BEGIN
75        offset = '   '
76        print, offset+'Return the proper IDL font positioning command for TeX'
77        print, offset+'sub/superscripts. '
78        print, offset+'fnt = sub_sup_idl( strn )'
79        print, offset+'Inputs:'
80        print, offset+offset+"strn      -- Either '^' or '_', the TeX super/subscript       in"
81        print, offset+offset+'             characters'
82        print, offset+'Keywords:'
83        print, offset+offset+'/FORCE_UD -- Set this to use !U/!D instead of !E/!I for'
84        print, offset+offset+'             sub/superscripts .'
85        print, offset+offset+'/HELP     -- Set to print useful message and exit.'
86        print, offset+'Outputs:'
87        print, offset+offset+"fnt       -- Either '!U' or !E' for superscripts,             out"
88        print, offset+offset+"             or '!D' or '!I' for subscripts."
89        return, -1
90    ENDIF
91
92    IF keyword_set(force_ud) THEN BEGIN
93        IF (token EQ '^') THEN return, '!U'
94        IF (token EQ '_') THEN return, '!D'
95        return, ''
96    ENDIF ELSE BEGIN
97        IF (token EQ '^') THEN return, '!E'
98        IF (token EQ '_') THEN return, '!I'
99        return, ''
100    ENDELSE
101   
102END
103
104;
105;+
106; NAME:
107;       TRANSLATE_SUB_SUPER
108; PURPOSE:
109;       Translate TeX sub/superscripts to IDL sub/superscripts.
110; CATEGORY:
111;       text/strings
112; CALLING SEQUENCE:
113;       new = translate_sub_super( old )
114; INPUTS:
115;       old       -- string to be translated from TeX to IDL.   in
116; KEYWORD PARAMETERS:
117;       /RECURSED -- set if this function is being called
118;                    recursively.                 
119;       /HELP     -- Set to print useful message and exit.
120; OUTPUTS:
121;       new       -- string old converted from TeX to IDL       out
122; COMMON BLOCKS:
123; SIDE EFFECTS:
124; NOTES:
125;       - For best results, when both a sub and superscript are used,
126;         place the shorter of the two first (e.g. 'N^{a}_{bbbb}' is
127;         better than 'N_{bbbb}^{a}').
128;       - Single character sub/super scripts do not need to be
129;         protected by braces.
130;       - Sub/superscripts may be nested (e.g. 'N^{N_1^N}').
131; EXAMPLE:
132;       out = translate_sub_super( 'N^2_{big}' )
133;       Then out='N!U2!N!Dbig!N' which looks like it should on the
134;       display.
135; LIBRARY FUNCTIONS CALLED:
136;       str_token      -- Text/string (mcraig)
137;       sub_sup_idl -- contained in this file
138; MODIFICATION HISTORY:
139;       $Id$
140;       $Log: translate_sub_super.pro,v $
141;       Revision 1.5  2000/06/14 19:09:22  mcraig
142;       Changed name of strtok str_token to avoid conflict in IDL 5.3.
143;
144;       Revision 1.4  1996/06/14 20:00:27  mcraig
145;       Updated Copyright info.
146;
147;       Revision 1.3  1996/05/09 00:22:17  mcraig
148;       Changed some function calls to reflect changes in those functions, moved
149;       some code out of the main loop that didn't need to be there, added
150;       documentation.
151;
152;       Revision 1.2  1996/02/08 18:54:20  mcraig
153;       Changed default sub/superscript size to be !D/!U rather than !I/!E to
154;       improve readability of plat annotations.
155;
156;       Revision 1.1  1996/01/31 18:47:37  mcraig
157;       Initial revision
158;
159; RELEASE:
160;       $Name: Rel_2_1_2 $
161;
162; COPYRIGHT:
163;  Copyright (C) 1996 The Regents of the University of California, All
164;  Rights Reserved.  Written by Matthew W. Craig.
165;  See the file COPYRIGHT for restrictions on distrubting this code.
166;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
167;-
168FUNCTION Translate_sub_super, InputString, $
169                              RECURSED=recursed, $
170                              HELP=Help
171
172; Return to caller if error.
173    On_error, 2
174
175; Offer help if needed and/or desired
176    IF (n_params() NE 1) OR keyword_set(help) THEN BEGIN
177        offset = '   '
178        print, offset+'Translate TeX sub/superscripts to IDL sub/superscripts.'
179        print, offset+'new = translate_sub_super( old )'
180        print, offset+'Inputs:'
181        print, offset+offset+'old       -- string to be translated from TeX to IDL.   in'
182        print, offset+'Keywords:'
183        print, offset+offset+'/RECURSED -- set if this function is being called '
184        print, offset+offset+'             recursively.                  '
185        print, offset+offset+'/HELP     -- Set to print useful message and exit.'
186        print, offset+'Outputs:'
187        print, offset+offset+'new       -- string old converted from TeX to IDL       out'
188        print, offset+'Notes:'
189        print, offset+offset+'- For best results, when both a sub and superscript are used,'
190        print, offset+offset+"  place the shorter of the two first (e.g. 'N^{a}_{bbbb}' is"
191        print, offset+offset+"  better than 'N_{bbbb}^{a}')."
192        print, offset+offset+'- Single character sub/super scripts do not need to be'
193        print, offset+offset+'  protected by braces.'
194        print, offset+offset+"- Sub/superscripts may be nested (e.g. 'N^{N_1^N}')."
195        return, -1
196    ENDIF
197
198;  To allow for nested scripts, use !E/!I instead of !U/!D for scripts
199;  when called recursively.
200    IF (NOT keyword_set(recursed)) THEN $
201      ud = 1 $
202    ELSE $
203      ud = 0
204
205;  Return to the normal level after making sub/superscript unless we
206;  are recursed, which indicates we are processing a nested script.
207    IF keyword_set(recursed) THEN fontRestore = '' ELSE fontRestore = '!N'
208
209;  Initialize vars for processing scripts.
210    SpcByte = (byte(' '))(0)    ;We need the BYTE value for a space below.
211    strn = InputString
212    pos = 0
213    StorePos = ''
214    RecallPos = ''
215    OldToken =  ''
216    LenLastScript = 0
217
218; Grab next sub/superscript.  Token will be either '^' or '_'.
219; RETURN if no scripts.
220    Token = nexttok(strn,  '^_', pos = pos)
221    if pos EQ -1 then return, InputString ;nothing to process.
222
223    FntChange =  sub_sup_idl(Token)
224
225; Our approach will be to grab the input string up to the next '^' or
226; '_', then process the script we've found.
227    NewString=str_token(strn,Token)
228
229    WHILE  strlen(strn) GT  0 DO  BEGIN
230;  Grab first char of sub/superscript.
231        Script = strmid(strn, 0, 1)
232        EndOfScript = 0         ;Position of end of this script.
233        IF (Script EQ '{') THEN BEGIN   ; Scripts of more than 1 char.
234            EndOfScript = matchdelim(strn)     
235            Script = translate_sub_super(strmid(strn, 1, EndOfScript-1), $
236                                         /recursed )
237        ENDIF
238;     Grab rest of string _after_ the end of the script.       
239        strn = strmid(strn, EndOfScript+1, $
240                      strlen(strn)-EndOfScript-1)
241
242;     Find the next script and prepare for processing it.
243        FntChange = sub_sup_idl(Token, FORCE_UD = ud)
244        OldToken = Token
245        Token = nexttok(strn, '^_', POS = pos)
246
247;     If the input is 'n^2_j', we want the '2' to be directly above
248;     the 'j', rather than having the 'j' below and to the right of
249;     the 2.  In other words, we want the first below, not the second.
250;              2               2
251;             N               N
252;              J                J
253;     To accomplish this, we need to save the position at which we
254;     begin writing the 2 with a !S, and restore that position with a
255;     !R after writing the 2.  The first section in the IF block below
256;     handles the 'J' above, the thing after the first script.  We
257;     don't care if there is another script following.  We also padd
258;     the second script with spaces if it is shorter than the first to
259;     make sure that whatever comes out after the scripts starts in
260;     the proper place.  The worry is that without the spaces, the
261;     input 'N^{looong}_{s} + 1' will end up with the + starting right
262;     the 's' ends.
263        IF (StorePos EQ '!S') THEN BEGIN
264            StorePos = ''
265            RecallPos = ''
266;     calculate the difference in length between this script and the
267;     previous stacked one, removing font change commands (crudely by
268;     guessing that the number of characters this takes is twice the
269;     number of exclamation points).  The  + 1 below is a kludge.  I
270;     don't know why, but I need one extra space.
271            NumSpaces = LenLastScript - (strlen(script) - 2*strcnt(Script,'!'))
272            NumSpaces = (NumSpaces + 1) > 0
273            IF NumSpaces GT 0 THEN $
274              Script = Script + string( replicate(SpcByte, NumSpaces) )
275        ENDIF ELSE BEGIN
276            IF (Token NE OldToken) AND (pos EQ 0) THEN BEGIN
277;             The next script immediately folows this one.  Arrange to
278;             save the position of the current script so that both begin
279;             with the same horizontal position.
280                StorePos = '!S'
281                RecallPos = '!R'
282                LenLastScript = strlen(Script) - 2*strcnt(Script,'!')
283            ENDIF
284        ENDELSE 
285
286;  Continue building the IDL string, adding on our just processed script.
287        NewString = NewString + StorePos + FntChange + Script + RecallPos $
288          + FontRestore
289
290        IF ( pos NE -1 ) THEN BEGIN     ; more left to process     
291            NewString = NewString $
292              + str_token(strn, Token)   
293        ENDIF ELSE BEGIN                ; we are done
294            NewString = NewString + strn
295            strn = ''
296        ENDELSE
297    ENDWHILE
298   
299    return, NewString
300END
301
302
303
304
Note: See TracBrowser for help on using the repository browser.