[첫화면으로]UseModWiki소스수정/LaTeX

마지막으로 [b]

LaTeX 수식 지원

LaTeX의 수식 기능을 사용하여 수식과 기호를 쉽게 표시하도록 함

$$y = x^2 + \frac{2}{3}\cdot x - 1$$이므로 다음 식이 성립한다. \[\displaystyle{\int\frac{e^{3x}}{1+e^{3x}}\,dx}=\displaystyle\frac13\ln(1+e^{3x})+\mathcal C.\] ok?
$$y = x^2 + \frac{2}{3}\cdot x - 1$$이므로 다음 식이 성립한다.
$\displaystyle{\int\frac{e^{3x}}{1+e^{3x}}\,dx}=\displaystyle\frac13\ln(1+e^{3x})+\mathcal C.$
ok?

config.pl 수정

다음 항목 수정 및 추가
$ENV{PATH}   = "/bin:/usr/bin/";     # 기존의 값 뒤에 콜론(:)을 써서 각 프로그램들이 있는 경로를 붙여적는다.
### LaTeX 변환 지원
$UseLatex    = 1;       # 1 = Use LaTeX conversion   2 = Don't convert

wiki.pl 수정

### 패치를 위해 추가된 환경설정 변수
use vars qw(
    ...
    $UseLatex   # 추가
    );
###

sub CommonMarkup {
    ...
        s/\&__LT__;code\&__GT__;((.|\n)*?)\&__LT__;\/code\&__GT__;/&StorePre($1, "code")/ige;

###############
### added by gypark
### LaTeX 지원  - 이 단락 추가
        if ($UseLatex) {
#           s/\$\$((.|\n)*?)\$\$/&StoreRaw(&MakeLaTeX("\$"."$1"."\$", "display"))/ige;
#           s/\$((.|\n)*?)\$/&StoreRaw(&MakeLaTeX("\$"."$1"."\$", "inline"))/ige;
            s/\\\[((.|\n)*?)\\\]/&StoreRaw(&MakeLaTeX("\$"."$1"."\$", "display"))/ige;
            s/\$\$((.|\n)*?)\$\$/&StoreRaw(&MakeLaTeX("\$"."$1"."\$", "inline"))/ige;
        }
###
###############
    ...
}

다음 함수 통채로 추가

sub UnquoteHtmlForPageContent {
    my ($html) = @_;
    $html =~ s/&__GT__;/>/g;
    $html =~ s/&__LT__;/</g;
    $html =~ s/&__AMP__;/&/g;
    $html =~ s/\\
/\\\\\n/g;
    $html =~ s/\
/\\\n/g;
    return $html;
}

다음 함수 통채로 추가

sub MakeLaTeX {
    my ($latex,  $type) = @_;

    $latex = &UnquoteHtmlForPageContent($latex);

    # 그림파일의 이름은 텍스트를 해슁하여 결정
    my $hash;
    my $hasMD5 = eval "require Digest::MD5;";
    if ($hasMD5) {
        $hash = Digest::MD5::md5_base64($latex);
    } else {
        $hash = crypt($latex, $HashKey);
    }
    $hash =~ s/(\W)/uc sprintf "_%02x", ord($1)/eg;

    # 기본값 설정
    my $hashimage = "$hash.png";
    my $imgpath = "";
    my $LatexDir = "$UploadDir/latex";
    my $LatexUrl = "$UploadUrl/latex";
    my $TemplateFile = "$DataDir/latex.template";

    # 디렉토리 생성
    &CreateDir($UploadDir);
    &CreateDir($LatexDir);

    if (-f "$LatexDir/$hashimage" && not -z "$LatexDir/$hashimage") {
        # 이미 생성되어 캐쉬에 있음
    } else {
        # 새로 생성해야 됨
        my $hashdir = "$TempDir/$hash";
        my $DefaultTemplate = << 'EOT';
\documentclass[12pt]{amsart}

% Your can use the desire symbol packages
% Of course, MikTeX needs to be able to get the
% package that you specify
\usepackage{mathptmx,bm,calrsfs}

% "sboxit" puts two marks on top and bottom of the math
% equation for ImageMagick to cut out the image
\def\sboxit#1{%
\setbox0=\hbox{#1}\hbox{\vbox{\hsize=\wd0\hrule height1pt width2pt%
\hbox{\vrule width0pt\kern0pt\vbox{%
\vspace{1pt}\noindent\unhbox0\vspace{1pt}}%
\kern1pt\vrule width0pt}\hrule height1pt width2pt}}}
\mathchardef\gt="313E % type $a\gt b$ instead of $a > b$
\mathchardef\lt="313C % type $a\lt b$ instead of $a < b$

\pagestyle{empty}
\begin{document}
\thispagestyle{empty}

% the first hbox make the depth of the equation right
\sboxit{\hbox to 0pt{\phantom{g}}<math>}

\end{document}
EOT

        if (not -d $hashdir) {
            mkdir($hashdir,0775) or return "[Unable to create $hash dir]";
        }

        if (not -f $TemplateFile) {
            &WriteStringToFile($TemplateFile, $DefaultTemplate);
        }

        my $template = &ReadFile($TemplateFile);

        $template =~ s/<math>/$latex/ige;

        my $pwd = `pwd`;
        $pwd =~ s/(.*)((\n|\r)*)?/$1/;

        chdir ($hashdir);

        # 원본 tex 생성
        open (OUTFILE, ">srender.tex");
        print OUTFILE $template;
        close OUTFILE;

        open SAVEOUT, ">&STDOUT";
        open SAVEERR, ">&STDERR";
        open STDOUT, ">hash.log";
        open STDERR, ">&STDOUT";

        # 그림 생성
        qx(latex -interaction=nonstopmode srender.tex);
        qx(dvips srender.dvi);
        qx(convert -transparent "white" -density 100x100 -trim -shave 0x2 srender.ps $hashimage);

        close STDOUT;
        close STDERR;
        open STDOUT, ">&SAVEOUT";
        open STDERR, ">&SAVEERR";

        # upload 경로 그림 옮김
        chdir($pwd);
        if (-f "$hashdir/$hashimage" && not -z "$hashdir/$hashimage") {
            my $png = &ReadFile("$hashdir/$hashimage");
            &WriteStringToFile("$LatexDir/$hashimage", $png);
        } else {
            return "[Error retrieving image from $hashdir:$pwd]";
        }
        unlink (glob("$hashdir/*")) or return "[[unlink fail]]";
        rmdir ($hashdir) or return "[[rmdir fail]]";
    }

    # IMG 태그 출력
    if ($type eq "inline") {
        $imgpath = "<IMG border=0 vspace=0 hspace=0 align='middle' ".
            "src='$LatexUrl/$hashimage' ".
            "alt=\"\$$latex\$\">";
    } elsif ($type eq "display") {
        $imgpath = "<br>".
            "<IMG border=0 vspace=15 hspace=40 align='middle' ".
            "src='$LatexUrl/$hashimage' ".
            "alt=\"$latex\">".
            "</br>";
    }
    return $imgpath;
}

추가 업데이트 내역

ext1.71 - UnquoteHtmlForPageContent 함수가 고쳐졌습니다.
-- Raymundo 2005-1-23 11:41 pm

ext1.74d - png 파일명을 결정하는 부분 개선. 특수 문자를 삭제하지 않고 아스키코드로 표현하게 함

-- Raymundo 2005-2-12 2:55 pm

ext1.74e - 1.74d 에서, 특수문자를 "%16진수"의 형태로 만들었더니 문제가 있어서, "_16진수"의 형태로 만들도록 함

-- Raymundo 2005-2-14 11:10 am

ext2.10 - 서버에 설치하기 힘든 경우 /LaTeX플러그인을 사용할 수 있게 함

-- Raymundo 2012-2-3 12:31 am

관련 링크

이 패치는 다음 세 가지 구현의 코드를 섞어서(-_-;) ext버전에 맞게 수정한 것이다.

사용자 의견

오. 드디어 들어가는 겁니까. 사실 저 같은 계정 사용자는 LaTeX를 실행시킬 수 있을지가 고민입니다. (예전에 해보려다 포기)
-- 조프 2005-1-14 10:32 am

저도 마찬가지로, 이 계정에는 tetex부터 설치를 해야 될 판이라... -_-; 이 작업은 제 연구실 서버에 따로 위키 깔아서 하고 있습니다. 제 홈에서는 예제를 보이기 힘들 것 같네요.
-- Raymundo 2005-1-14 10:35 am

[여기]에 현재까지 작업된 결과를 볼 수 있습니다.

-- Raymundo 2005-1-14 6:16 pm

왕입니다요 !!!

-- Bab2 2005-1-14 6:33 pm

LaTeX 변환을 위한 문법을 어떻게 해야 될지 고민 중입니다.

1) usemod 구현
$a<b$   - 문장 내에 삽입
$$a<b$$ - 별개의 줄로
    LaTeX과 동일한 방식이라 LaTeX 코드를 그대로 쓸 수 있음.
    그러나 $a<b$ 의 경우 "$"가 다른 의미로 쓰인 곳에서도 LaTeX변환이 이뤄질 경우가 많음 (ex. "$UploadDir, $UploadUrl 을 설정한다"와 같은 경우)
2) oddmuse 구현
$$a<b$$ - 문장 내에 삽입
\[a<b\] - 별개의 줄로
    다른 데서 $$ 나 \[ 를 사용할 일이 거의 없기 때문에 오변환의 가능성이 적지만 LaTeX에서의 문법과 의미가 달라진다.
3) oddmuse 에서 별도 옵션 사용
$a<b$   - 문장 내에 삽입
$$a<b$$ - 별개의 줄로
\[a<b\] - 별개의 줄로

4) moniwiki 구현
$(공백)a<b$(공백) - 문장 내에 삽입
    공백이 반드시 있어야 하기 때문에 1)에서와 같은 오변환의 가능성이 적어짐
저는 지금부터 자잘한 수정에 들어갈테니 이 글 보시면 의견 좀 주세요. (근데 의견달라고 하면 왜 다들 조용히 계시는지? :-)
-- Raymundo 2005-1-14 8:04 pm

$$와 \[ 로 결정

-- Raymundo 2005-1-15 12:35 am

뭔가 좀 부족한 게 있을 듯 합니다만 일단 여기까지 하고 마칩니다~

-- Raymundo 2005-1-15 1:21 am
이름:  
Homepage:
내용:
 

위키위키분류
각주:
1. 주인장이 정확히 아는 것은 아니고 테스트해보니 그랬다

마지막 편집일: 2012-2-3 12:31 am (변경사항 [d])
1658 hits | Permalink | 변경내역 보기 [h] | 페이지 소스 보기