Stats

Popular Posts

Followers

Mathematica 教學 輸出講義所需的TeX語法

戴忠淵 於 2013年6月21日星期五 上午12:21 發表


懶惰是一種美德

handoutform[command_String] := Block[{text},
(*判斷語法是否多行*)
   text = StringSplit[command, "\n"];
(*輸出Mathematica計算結果*)
   Print@ToExpression[command];
(*若程式僅一行,TeX直接跳到下一列;若程式兩行,第一行換行第二行跳列;若三行中間行僅換行*)
   StringReplace["\\begin{mathenv}\n\\memo{}\n" <>
     Which[
      Length[text] == 1, 
      "\\mathin\n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
       "|\\\\\n" ,
      Length[text] == 2, 
      "\\mathin \n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
       "|\\newline \n\\verb|" <> StringReplace[text[[2]], " " -> ""] <>
        "|\\\\\n" ,
      Length[text] >= 3, 
      "\\mathin \n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
       "|\\newline\n\\verb|" <> 
       StringJoin[
        StringReplace[#, " " -> ""] <> "|\\newline\n\\verb|" & /@ 
         text[[2 ;; -2]]] <> StringReplace[text[[-1]], " " -> ""] <> 
       "|\\\\\n"]
     <> "\\mathout $" <> ToString[TeXForm[ToExpression[command]]] <> 
     "$\n\\end{mathenv}", {"e^" -> "\\ex^"}]
   ];

(*設定圖形目錄*)
SetDirectory[];

handoutplotform[command_String, figurename_String] := Block[{text},
  text = StringSplit[command, "\n"];
  Print@ToExpression[command];
(*輸出圖形至指定目錄*)
  Export[figurename <> ".eps", ToExpression[command]];
  "\\begin{mathenv}\n\\memo{}\n" <>
   Which[
    Length[text] == 1, 
    "\\mathin \n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
     "|\\\\\n" ,
    Length[text] == 2, 
    "\\mathin \n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
     "|\\newline\n\\verb|" <> StringReplace[text[[2]], " " -> ""] <> 
     "|\\\\\n" ,
    Length[text] >= 3, 
    "\\mathin \n\\verb|" <> StringReplace[text[[1]], " " -> ""] <> 
     "|\\newline\n\\verb|" <> 
     StringJoin[
      StringReplace[#, " " -> ""] <> "|\\newline\n\\verb|" & /@ 
       text[[2 ;; -2]]] <> StringReplace[text[[-1]], " " -> ""] <> 
     "|\\\\\n"]
   <> "\\mathplot{" <> figurename <> "}\n\\end{mathenv}"
  ]

(*Example*)
handoutplotform["Plot[Sin[x],{x,0,2Pi}]", "test"]

handoutform["Integrate[24*x*y,{x,0,0.5},{y,1-x,1}]+
 Integrate[24*x*y,{x,0.5,1},{y,1-x,1.5-x}]"]

handoutform["PDF[NormalDistribution[],x]"]

Version 2.

Options[handoutform]={"Method"->"unList"};
handoutform[command_String,OptionsPattern[]]:=
Block[{myout,text,outputtex,arraytext,a,b,x,newlinepos,
newlinesign,listtex,method},

(*選項*)

Options[handoutform]={"Method"->"unList"};
method=OptionValue["Method"];

(*判斷語法是否多行*)

myout=ToExpression[command];

text=StringSplit[command,"\n"];

(*輸出Mathematica計算結果*)

outputtex=ToString@TeXForm[StandardForm@ToExpression[command]];
arraytext=
"{l"<>StringReplace[
StringCases[outputtex,"\\begin{array}{c"~~x__~~"c}"->x],
"c"->"l"]<>"l}";

listtex=If[method=="List",
newlinepos=StringPosition[outputtex,"\\}"];
newlinepos=
1+Accumulate[
Length/@
NestWhileList[
Cases[newlinepos,{a_,b_}/;#[[-1,2]]<
b<#[[-1,2]]+95]&,{{0,0}},#[[-1,2]]!=
newlinepos[[All,2]][[-1]]&][[2;;-2]]];
Print@myout;
TeXForm[Insert[myout,newlinesign,Partition[newlinepos,1]]],
Print@myout];

listtex=
StringReplace[ToString@listtex,
",\\text{newlinesign},"->"$,\\newline\n$"];

(*若程式僅一行,TeX直接跳到下一列;若程式兩行,第一行換行第二行跳列;若三行中間行僅換行*)

StringReplace[
"\\begin{mathenv}\n\\memo{}\n"<>
Which[Length[text]==1,
"\\mathin\n\\verb|"<>StringReplace[text[[1]],""->""]<>
"|\\\\\n",Length[text]==2,
"\\mathin\n\\verb|"<>StringReplace[text[[1]],""->""]<>
"|\\newline\n\\verb|"<>StringReplace[text[[2]],""->""]<>
"|\\\\\n",Length[text]>=3,
"\\mathin\n\\verb|"<>StringReplace[text[[1]],""->""]<>
"|\\newline\n\\verb|"<>
StringJoin[
StringReplace[#,""->""]<>"|\\newline\n\\verb|"&/@
text[[2;;-2]]]<>StringReplace[text[[-1]],""->""]<>
"|\\\\\n"]<>"\\mathout\n$"<>
If[method=="List",listtex,outputtex]<>"$\n\\end{mathenv}",

(*取代字串*)

{"e^"->"\\ex^",
"dx$"->"\\dx$",
"dy$"->"\\dy$",
"dz$"->"\\dz$",
"du$"->"\\du$",
"dv$"->"\\dv$",
"\\text{"~~ShortestMatch[a___]~~"}"->a,
"{c"~~ShortestMatch[b___]~~"c}"->arraytext,
"$\\begin{array}"->"\\matharray{\n\\begin{array}",
"\\end{array}$"->"\\end{array}\n}"}]];

(* Example *)

handoutform["N@Table[{x,Sin[x]},{x,0,2Pi,Pi/10}]"]
handoutform["N@Table[{x,Sin[x]},{x,0,2Pi,Pi/10}]","Method"->"List"]


Tags: , ,

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~