// -*- ASCII:EDT -*- // uncommand.edt // (schlicht 20040318) ///////////////////////// // ------------------------------------------------------------------------- // This macro expands all custom LaTeX commands in your file. // ------------------------------------------------------------------------- // Sometimes, it may be necessary to remove your custom commands, e.g. if you // want to submit your TeX source to journals that don't allow custom commands. // This macro will // * gather all commands defined with \newcommand in the preamble and // * replace them in the current file. // * It can also retain the knowledge about the original commands, so // that it is even possible to change a command definition later on, // or to restore the original commands. // // Examples: If you have //\newcommand{\A}{\mathcal{A}} // in the document's preamble, all "\A" strings will be replaced by "\mathcal{A}". // Likewise, for //\newcommand{\abs}[1]{\left\vert #1\right\vert} // all "\abs" strings will be replaced by "\left\vert #1\right\vert", // where "#1" is the respective argument to "\abs". // // Some remarks: // * The macro will only gather those commands that are defined in the // preamble, i.e., before "\begin{document}". // * Also, it will only gather them, if the string "\newcommand" starts at // the beginning of a line (that's a feature -- thus, you can have some // commands ignored when expanding). // * Command definitions with "\def", "\renewcommand" etc. will not be recognized. // * Be careful with spaces before parameters. E.g., in the above example, // writing "\newcommand{\abs}[1]{\left\vert#1\right\vert}" would be equally // correct, as far as LaTeX is concerned. However, if it is being expanded, // the argument would follow immediately after "\vert" (e.g., "\verta", if "a" // is the argument), which would be an undefined control sequence. Therefore, // you should insert a space before those parameters. // * If a custom command contains other custom commands in its definition, you // must put its definition *before* the definitions of those that it contains. // E.g., it should be: // \newcommand{\AxA}{\A\times\A} // \newcommand{\A}{\mathcal{A}} // * If the current file is an included file, you must set the "Main File", // so that preamble can be found. // * If the document is soft-wrapped, you should unwrap it before running this macro. // // Options: // -------- // The macro will record the original commands, if you change "no" to "yes", here: Assign("UNC_record","no"); // "yes" or "no" // Then, for each replacement, a comment will be inserted, containing the original // command. This will clutter your file, but it makes the whole affair revertable: // * You can change a \newcommand definition afterwards, and then re-run the // macro on your file, which will change all expansions accordingly. (= "change") // (Hint: Indent the unchanged definitions, so that they will be ignored.) // * You can also replace the expansions with the original commands (= "restore") // Note that changing and restoring commands isn't completely reliable. // * Finally, you can remove all comments, e.g. when you want to submit the file (= "clean") // The default behaviour is to replace commands with their expansions (= "new"). Assign("UNC_action","new"); // "new", "change", "restore", or "clean" // ------------------------------------------------------------------------- Requires(20020802); // get preamble GetBuild(0); IfStr("%!0","20020802",">",> !"GetDocAsASCII(0,'%P\%N%T',0,0);",> !"ReadFile('%P\%N%T',0);"); FindInString("%!0","<@{ }\\begin\{document\}",1,2,1); IfOK(!"ExtractByIndex(1,'%!0',0,%!1);",> !"Prompt('No \begin{document} found.%\Don''t know what to do!%\%\> (If this is an included file, set the Main File.)');> Exit;"); // skip preamble BeginGroup; SaveFind; SetFindTextSelect(1); SetSearchCurrentDoc; SetSearchForward(1); SetSearchCaseSensitive(1); SetSearchEntire(0); SetNotFoundNotify(0); SetSearchCyclic(0); SetSearchWholeWords(0); SetSearchInline(0); SetSearchRelaxed(0); SetRegEx(1); SetFindStr("<@{ }\\begin\{document\}"); SetTracking(0); Mark(1); CMD("Go To Beginning Of File"); Find; Mark(0); LetRegNum(8,0); LetRegNum(9,0); // parse command definitions Assign("UNC_getcmd",!`> SetOK(1);> FindInString("%!1","<\\newcommand@{ }@{\*@{ }}{\{\(9+^\}\)\}}|{\(9\\+^{\[|\{}\)}\(8@{@{ }\[+{^\]}\]@{ }\(7@{\[@{^\]}\]}\)}\)@{ }\{\(6@{>|{\{@{>|{\{@{>|{\{@{>|{\{*\}}|^{\}}}\}}|^{\}}}\}}|^{\}}}\}}|^{\}}}\)\}",3,9,1011,%!9);> IfOK(!"> GetTag(9,3);> Assign('UNC_cmd','%!3');> GetTag(8,3);> IfStr('%!3','','>',!'> FindInString(''%!3'',''\[\(+^\])\]'',4,5,1);> ExtractByIndex(3,""%!3"",%!4+1,%!5-1);> Assign(''UNC_nr'',''%!3'');> ','Assign(''UNC_nr'',''0'');');> GetTag(7,3);> IfStr('%!3','','>',!'> Assign(''UNC_defNR'',''1'');> GetLength(4,''%!3'');> ExtractByIndex(3,''%!3'',1,%!4-2);> Assign(''UNC_def'',''%!3'');',> 'Assign(''UNC_defNR'',''0'');> Release(''UNC_def'');');> GetTag(6,3);> Assign('UNC_exp','%!3');> Release('UNC_args');> IfStr('%$(|UNC_nr|);','0','>',!'> IfStr(''%$(|UNC_defNR|);'',''0'',''<>'',> ''Assign(|UNC_args|,``@{ }@{\[\(1@^\]\)\]}``);'');> LetRegNum(6,''1+%$(|UNC_defNR|);'');> Repeat(''%$(|UNC_nr|);-%$(|UNC_defNR|);'',!''> Assign(|UNC_args|,``%$(*UNC_args*);@{ }\{\(%!6@{>|{\{@{>|{\{@{>|{\{@{>|{\{@{>|{\{*\}}|^{\}}}\}}|^{\}}}\}}|^{\}}}\}}|^{\}}}\}}|^{\}}}\)\}``);> LetRegNum(6,%!6+1);> '');> ','Assign(''UNC_args'',''~{$Alpha$}'');');> // search for \cmd or its expansion? IfStr('%$(|UNC_action|);','change','=',> !'Assign(''UNC_search'',''%% START Macro: \%$(|UNC_cmd|);%$(|UNC_args|);><**%% END Macro: \%$(|UNC_cmd|);>'');',> !'Assign(''UNC_search'',''\#{<@^{%%|\%$(|UNC_cmd|);{$~Alpha$|>}}}\%$(|UNC_cmd|);%$(|UNC_args|);'');> IfStr(''%$(|UNC_record|);'',''yes'',''='',!''Assign(|UNC_search|,|%$(*UNC_search*);@{@{ }\(2>\)}|);'');');> ","Release('UNC_cmd');");> `); // find commands in document Assign("UNC_docmd",!`> StartWorking("Replacing ""%$('UNC_cmd');"" ...");> SetFindStr("%$('UNC_search');");> SearchReset;> IfFound(!"> Do('%$(|UNC_getargs|);');> // record original command? IfStr('%$(|UNC_record|);','yes','=',!'> Do(''%$(|UNC_rmCR|);'');> InsText(''%% START Macro: %!2%\'');> GetTag(2,3);> IfStr(''%!3'','''',''<>'',''LetReg(3,|%%\|);'');> InsText(''%!7%% END Macro: %$(|UNC_cmd|);%!3%\'');',> !'InsText(''%!7'');');> SetSel(0);> ",> "LetRegNum(8,0);> Release('UNC_exp');> Goto(0);");> `); // replace command Assign("UNC_getargs",!`> LetReg(2,"%$('UNC_cmd');");> LetReg(7,"%$('UNC_exp');");> IfStr("%$('UNC_nr');","0",">",!"> // expand arguments LetRegNum(6,1);> Repeat('%$(|UNC_nr|);',!'> GetTag(%!6,5);> // optional argument? IfNum(''%$(*UNC_defNR*);%!6'',11,''='',!''> IfStr(|%!5|,||,|=|,!|LetReg(5,*%$(:UNC_def:);*);|);> LetReg(2,|%!2 [%!5]|);'',> !''LetReg(2,|%!2 {%!5}|);'');> LetRegNum(4,0);> Loop(!''> SetOK(1);> FindInString(|%!7|,|#%!6|,3,4,1000,%!4);> IfOK(!|ReplaceInString(*%!7*,*%!5*,%!3,%!4,0,7);|,> |Stop;|);> '');> LetRegNum(6,%!6+1);> ');> ");> `); // remove newlines Assign("UNC_rmCR",!`> Loop(!"SetOK(1);> FindInString('%!2','@{%%*}>',3,4,1);> IfOK(!'ReplaceInString(''%!2'','''',%!3,%!4,0,2);','Stop;');");> `); // removing comments is easy IfStr("%$('UNC_action');","clean","=",!"> SetFindStr('%% {START|END} Macro: *>');> SearchReset;> Assign('UNC_cmd','comment');> Assign('UNC_getcmd','IfFound(''CMD(|Delete|);'',''Release(|UNC_cmd|);'');');> Assign('UNC_docmd','Stop;');> StartWorking('cleaning...');"); // re-inserting commands hardly less... IfStr("%$('UNC_action');","restore","=",!"> SetFindStr('%% START Macro: \(0+^{>| }\)@{ \(1+^>\)}><**%% END Macro: \0>');> SearchReset;> Assign('UNC_cmd','command');> Assign('UNC_getcmd',!'IfFound(!''GetTag(0,7);GetTag(1,8);InsText(|%!7%!8|);'',''Release(|UNC_cmd|);'');');> Assign('UNC_docmd','Stop;');> StartWorking('restoring...');"); // the main loop Loop(!`> Do("%$('UNC_getcmd');");> IfStr("%$('UNC_cmd');","","=","Stop;",> !"Loop(!'> Do(''%$(|UNC_docmd|);'');> IfStr(''%$(|UNC_exp|);'','''',''='',''Stop;'');> ');> ");> `); Goto(1); SetSel(0); SetTracking(1); Refresh; RestoreFind; Release("UNC_args"); Release("UNC_cmd"); Release("UNC_def"); Release("UNC_defNR"); Release("UNC_exp"); Release("UNC_nr"); Release("UNC_search"); StopWorking; EndGroup; End; $Id: uncommand.edt,v 1.2 2007-04-03 22:42:58+02 schlicht Exp schlicht $