Автор Тема: Обработка текста из командной строки  (Прочитано 2829 раз)

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
Подскажите, пожалуйста, как из командной строки либреофисом или ещё каким средством конвертировать формулу в отрисованную формулу.
В либреоффисе есть редактор формул. Как ему передать через командную строку x^2, чтобы в ответ получить картинку или векторный рисунок x2?
Или может есть какая-нибудь библиотека TeX скоторую можно так из командной строки использовать?

Онлайн Skull

  • Глобальный модератор
  • *****
  • Сообщений: 19 908
    • Домашняя страница
    • Email
latex
Андрей Черепанов (cas@)

Оффлайн Александр Ерещенко

  • Завсегдатай
  • *
  • Сообщений: 1 153
https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters/ru
И там есть указание на ключ:
--convert-to output_file_extension[:output_filter_name] [--outdir output_dir] filesВот только сам по себе LO Math экспортировать умеет вроде только в pdf

Вот еще интересная ссылка завалялась на эту тему:
http://www1.chapman.edu/~jipsen/mathml/asciimath.xml
« Последнее редактирование: 20.02.2017 19:46:06 от Alexander Yereshenko »

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
latex
Как этим воспользоваться для вышеописанной цели? Есть мануал или может просто известна простая команда командной строки?

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters/ru
И там есть указание на ключ:
--convert-to output_file_extension[:output_filter_name] [--outdir output_dir] filesВот только сам по себе LO Math экспортировать умеет вроде только в pdf

Вот еще интересная ссылка завалялась на эту тему:
http://www1.chapman.edu/~jipsen/mathml/asciimath.xml
--convert-to я знаю, и даже пробовал, только проблема в том, что он как видит так и конвертирует. В формулу не превращает. Если ему дать x^2 он так и сделает pdf с x^2, а мне нужно x2

Оффлайн Александр Ерещенко

  • Завсегдатай
  • *
  • Сообщений: 1 153
А какая конечная цель использования графического представления формул? Может, найдется другой метод решения конечной задачи?

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
А какая конечная цель использования графического представления формул? Может, найдется другой метод решения конечной задачи?
Хочу в программе отображать красиво введённые формулы, а изобретать велосипед не хочется.

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
Уже нашёл два решения:
//==================================================================================================
// T e x 2 p n g                                                                     Implementation
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This program is free software; you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details (http://www.gnu.org).

// This programs allows to convert a LaTeX formula into a PNG image.

// You will need LaTeX, GhostScript and Linux facilities to use this program. The optional
// graphical display requires Java classes from the B++ Library.

// Headers //---------------------------------------------------------------------------------------
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

// Types //-----------------------------------------------------------------------------------------
typedef std::ifstream InputFile;
typedef std::ofstream OutputFile;
typedef std::string   String;

typedef bool          boolean_t;
typedef unsigned char byte_t;
typedef unsigned int  cardinal_t;
typedef char          character_t;
typedef const char *  cstring_t;
typedef long          integer_t;
typedef double        real_t;
typedef char *        string_t;

// Portability //-----------------------------------------------------------------------------------
// #define or  ||
// #define and &&
// #define not !

// Global Variables //------------------------------------------------------------------------------
String    bpp_java       = "";
boolean_t display        = false;
boolean_t lowQuality     = false;
boolean_t percentReplace = false;
boolean_t quiet          = true;
real_t    scale          = 20;
boolean_t secureProgram  = false;
boolean_t starReplace    = false;
String    temporary      = "tex2png";

String auxFile;
String dviFile;
String epsFile;
String logFile;
String pngFile;
String pnmFile;
String psFile;
String outFile;
String texFile;

// Functions Implementation //----------------------------------------------------------------------

//------------------------------------------------------------------------------------------CutImage
void cutImage(cstring_t _inputFile,cstring_t _outputFile) {
 character_t  character;
 InputFile    fin;
 OutputFile   fout;
 boolean_t ** image;
 String       word;

 cardinal_t n1;
 cardinal_t x1;
 cardinal_t y1;

 cardinal_t height;
 cardinal_t left;
 cardinal_t top;
 cardinal_t width;

 cardinal_t border = 4;
 cardinal_t bottom = 0;
 cardinal_t right  = 0;
 cardinal_t x2     = 0;
 cardinal_t y2     = 0;

 // Image Matrix Reading //
 fin.open(_inputFile,std::ios::in|std::ios::binary);
 fin >> word;

 while (not fin.eof() and word!="(device=pnm)") fin >> word;
 if (fin.eof()) return;

 fin >> x1 >> y1;
 image=new boolean_t *[y1];
 image[0]=new boolean_t[x1];

 left=x1-1;
 top=y1-1;
 n1=x1*y1;

 while (n1>0) {
  --n1;

  // Pixel Reading //
  do fin.read(&character,1);
  while ((byte_t)character<=32);

  image[y2][x2]=(character=='1');

  // Bounding Box Computation //
  if (image[y2][x2]) {
   left=std::min(x2,left);
   right=std::max(x2,right);
   top=std::min(y2,top);
   bottom=std::max(y2,bottom);
  }

  // Next Line //
  if (++x2 == x1) {
   x2=0;
   if (++y2 < y1) image[y2]=new boolean_t[x1];
  }
 }

 fin.close();

 // PNM Image Writing //
 fout.open(_outputFile);
 fout << "P1" << std::endl;
 fout << "# Image generated by TeX2PNG" << std::endl;

 height=(bottom-top+1)+2*border;
 width=(right-left+1)+2*border;
 fout << width << " " << height << std::endl;

 // Top Border //
 y2=0;

 while (y2<border) {
  x2=0;
  while (x2++<width) fout << '0';
  fout << std::endl;
  ++y2;
 }

 // Bounded Image //
 y2=0;

 while (y2<y1) {
  if (y2>=top and y2<=bottom) {
   // Left Border //
   x2=0;
   while (x2++ < border) fout << '0';

   // Line //
   x2=left;
   while (x2<=right) fout << (image[y2][x2++] ? '1' : '0');

   // Right Border //
   x2=0;
   while (x2++ < border) fout << '0';

   // End Of Line //
   fout << std::endl;
  }

  delete [] image[y2++];
 }

 delete [] image;

 // Bottom Border //
 y2=0;

 while (y2<border) {
  x2=0;
  while (x2++<width) fout << '0';
  fout << std::endl;
  ++y2;
 }

 // File Closing //
 fout << std::endl;
 fout.close();
}

//------------------------------------------------------------------------------GenerateFormulaImage
void generateFormulaImage(cstring_t _outputFile,cstring_t _formula) {
 String      command;
 InputFile   fin;
 OutputFile  fout;
 cardinal_t  position;
 character_t resolution[32];
 character_t size[32];
 String      word;

 integer_t x1;
 integer_t x2;
 integer_t y1;
 integer_t y2;

 String formula;
 String outputFile;

 // File Name Normalizing //
 while (*_outputFile) {
  if (*_outputFile=='\\') outputFile+='/';
  else outputFile+=*_outputFile;

  ++_outputFile;
 }

 // Formula Construction //
 while (*_formula != 0) {
  if (starReplace and *_formula=='*') formula+="{\\times}";
  else if (secureProgram and *_formula=='"') formula+="";
  else formula+=*_formula;

  ++_formula;
 }

 if (percentReplace) {
  character_t ascii[] = { 0,0 };
  character_t hexa[]  = { '%','0','0',0 };

  while ((byte_t)ascii[0]<255) {
   ++(ascii[0]);

   if (hexa[2]=='9') hexa[2]='A';
   else if (hexa[2]=='F') {
    if (hexa[1]=='9') hexa[1]='A';
    else ++hexa[1];

    hexa[2]='0';
   }
   else ++hexa[2];

   while ((position=formula.find(hexa)) != String::npos) formula.replace(position,3,ascii);
  }
 }

 // LaTeX Generation //
 std::cout << "[>] LaTeX File Generation..." << std::endl;
 fout.open(texFile.c_str(),std::ios::in|std::ios::trunc);

 fout << "\\documentclass[a4paper,11pt]{article}" << std::endl
      << "\\usepackage{amssymb}" << std::endl
      << "\\usepackage{amsmath}" << std::endl
      << "\\usepackage{epsfig}" << std::endl
      << "\\pagestyle{empty}" << std::endl
      << "\\textwidth 15cm" << std::endl
      << "\\setlength{\\parindent}{0mm}" << std::endl
      << "\\begin{document}" << std::endl
      << "$\\displaystyle " << formula << "$" << std::endl
      << "\\end{document}" << std::endl;

 fout.close();

 if (fout.fail()) {
  std::cerr << "[!] Can't create the LaTeX file." << std::endl;
  exit(1);
 }

 // LaTeX Compilation //
 std::cout << "[>] LaTeX File Compilation..." << std::endl;
 command="latex -interaction=nonstopmode "+texFile;
 if (quiet) command+=" > "+outFile+" 2> "+outFile;
 system(command.c_str());
 command="touch "+dviFile;
 system(command.c_str());
 remove(texFile.c_str());
 remove(auxFile.c_str());
 remove(logFile.c_str());

 // EPS Generation //
 std::cout << "[>] PostScript File Generation..." << std::endl;
 command="dvips -q -D 600 -E -n 1 -p 1 -o "+epsFile+" "+dviFile;
 if (quiet) command+=" > "+outFile+" 2> "+outFile;
 system(command.c_str());
 command="touch "+epsFile;
 system(command.c_str());
 remove(dviFile.c_str());

 // Bounding Box Computation //
 fin.open(epsFile.c_str(),std::ios::in);

 do fin >> word;
 while (not fin.eof() and word!="%%BoundingBox:");

 if (word=="%%BoundingBox:") fin >> x1 >> y1 >> x2 >> y2;
 fin.close();

 if (fin.fail()) {
  std::cerr << "[!] Can't find the bounding box." << std::endl;
  exit(1);
 }

 if (secureProgram) {
  if ((x2-x1)*scale*0.25 > 1600 or (y2-y1)*scale*0.25 > 1200) {
   std::cerr << "[!] Sorry, image too big." << std::endl;
   exit(1);
  }
 }

 // PS Generation //
 fout.clear();
 fout.open(psFile.c_str(),std::ios::in|std::ios::trunc);

 fout << "1 1 1 setrgbcolor" << std::endl
      << "newpath" << std::endl
      << "-1 -1 moveto" << std::endl
      << (x2-x1+2) << " -1 lineto" << std::endl
      << (x2-x1+2) << " " << (y2-y1+2) << " lineto" << std::endl
      << "-1 " << (y2-y1+2) << " lineto" << std::endl
      << "closepath" << std::endl
      << "fill" << std::endl
      << -x1 << " " << -y1 << " translate" << std::endl
      << "0 0 0 setrgbcolor" << std::endl
      << "("+epsFile+") run" << std::endl;

 fout.close();

 if (fout.fail()) {
  std::cerr << "[!] Can't generate the PS file." << std::endl;
  exit(1);
 }

 // PNG Generation //
 std::cout << "[>] PNG Image Generation..." << std::endl;

 if (lowQuality) {
  scale*=0.25;
  sprintf(size,"%dx%d",(int)((x2-x1)*scale),(int)((y2-y1)*scale));
  sprintf(resolution,"%dx%d",(int)(scale*72),(int)(scale*72));
  command=String("gs -q -g")+size+" -r"+resolution;
  command+=" -sDEVICE=pnggray -sOutputFile=";
  command+=outputFile;
  command+=" -dNOPAUSE -dBATCH -- "+psFile+";";

  if (quiet) command+=" > "+outFile+" 2> "+outFile;
  system(command.c_str());

  remove(epsFile.c_str());
  remove(psFile.c_str());
 }
 else {
  sprintf(size,"%dx%d",(int)((x2-x1)*scale),(int)((y2-y1)*scale));
  sprintf(resolution,"%dx%d",(int)(scale*72),(int)(scale*72));
  command=String("gs -q -g")+size+" -r"+resolution;
  command+=" -sDEVICE=pnm -sOutputFile="+pnmFile+" -dNOPAUSE -dBATCH -- "+psFile+";";

  if (quiet) command+=" > "+outFile+" 2> "+outFile;
  system(command.c_str());

  remove(epsFile.c_str());
  remove(psFile.c_str());

  cutImage(pnmFile.c_str(),pnmFile.c_str());

  command="pnmscale 0.25 "+pnmFile+" 2> "+outFile+" | pnmtopng > ";
  command+=outputFile;
  if (quiet) command+=" 2> "+outFile;
  system(command.c_str());
  remove(pnmFile.c_str());
 }

 remove(outFile.c_str());

 // Image Display //
 if (display) {
  if (bpp_java=="") {
   if (std::getenv("BPP_JAVA")) bpp_java=String(std::getenv("BPP_JAVA"));
   else bpp_java=".";
  }

  std::cout << "[>] Image Display..." << std::endl;
  command="java -cp \""+bpp_java+"\" bpp.graphic.PictureFrame ";
  command+=outputFile;
  system(command.c_str());
  remove(pngFile.c_str());
 }
}

//----------------------------------------------------------------------------------------------Main
int main(int argc,const char * argv[]) {
 String formula;
 String filename;

 int i = 1;

 while (i<argc) {
  if (argv[i][0]=='-') {
   switch (argv[i][1]) {
    case 'b': bpp_java=(argv[i]+2); break;
    case 'r':
     if (argv[i][2]=='*') starReplace=true;
     if (argv[i][2]=='%') percentReplace=true;
    break;
    case 's': scale=atof(argv[i]+2); break;
    case 't': temporary=(argv[i]+2); break;
    case 'v': quiet=false; break;
    case 'd': display=true; break;
    case 'l': if (argv[i][2]=='q') lowQuality=true; break;
    default: std::cout << "[!] Unknown option '" << argv[i] << "'" << std::endl;
   }
  }
  else {
   if (formula=="") formula=argv[i];
   else filename=argv[i];
  }

  ++i;
 }

 if (secureProgram) {
  display=false;
  if (scale>20) scale=20;
 }

 texFile=temporary+".tex";
 outFile=temporary+".out";
 dviFile=temporary+".dvi";
 auxFile=temporary+".aux";
 logFile=temporary+".log";
 epsFile=temporary+".eps";
 psFile=temporary+".ps";
 pnmFile=temporary+".pnm";
 pngFile=temporary+".png";

 if (display and filename=="") filename=pngFile;

 if (formula=="" or (not display and filename=="")) {
  std::cout << "[!] Syntax: " << argv[0] << " [options] <formula> [output file]" << std::endl
            << std::endl
            << "    Options:" << std::endl
            << "      -bx = sets the path of the B++ Library Java classes to x." << std::endl
            << "      -d  = displays the image, the output file is optional." << std::endl
            << "      -lq = low quality, faster." << std::endl
            << "      -r* = replaces the '*' symbol by the '\\times' command." << std::endl
            << "      -r% = replaces the '%XX' hexa sequences by the corresponding ASCII symbol." << std::endl
            << "      -sx = sets the scale of the image to x (real value)." << std::endl
            << "      -tx = sets the prefix of the temporary files." << std::endl
            << "      -v  = verbose mode." << std::endl;

  exit(1);
 }

 generateFormulaImage(filename.c_str(),formula.c_str());
 return 0;
}

// End //-------------------------------------------------------------------------------------------

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
И на баше:
#!/bin/bash

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

BACKGROUND=White
BORDER=
DPI=
ENVIRONMENT=\$
FOREGROUND=Black
FORMULA=
HEADER=
HELP=0
MARGIN=
META=1
OPTIMIZE=0
PACKAGES=
PADDING=
PNGFILE=
SHOWVERSION=0
SILENT=0
SIZE=11
TEXFILE=
declare -r VERSION=0.10

function box {
    if [ "$PADDING" ]; then
        convert $PNGFILE -bordercolor $BACKGROUND -border $(scale $PADDING) $PNGFILE
    fi

    if [ "$BORDER" ]; then
        convert $PNGFILE -bordercolor $BORDER -border $(scale 1) $PNGFILE
    fi

    if [ "$MARGIN" ]; then
        convert $PNGFILE -bordercolor $BACKGROUND -border $(scale $MARGIN) $PNGFILE
    fi
}

function clean {
    local BNAME=${TEXFILE%.tex}
    rm -f $TEXFILE $BNAME.aux $BNAME.dvi $BNAME.log
}

function collapse {
    echo $1 | sed -e 's/[[:space:]]+/ /'
}

function dpi {
    local VALUE

    if exists xdpyinfo; then
        VALUE=$(xdpyinfo 2> /dev/null | grep resolution | sed -r 's/^[^0-9]+([0-9]+)x.*$/\1/')
    fi

    if [ ! "$VALUE" ]; then
        VALUE=96
    fi

    echo $VALUE
}

function exists {
    local COMMAND=$1
    return $(command -v $COMMAND &> /dev/null)
}

function generate {
    local BNAME
    local BEGINENV
    local ENDENV
    local PREFIX
    local MESSAGE
    local TMPDIR
    local SUFFIX

    if [ ! "$FORMULA" ]; then
        MESSAGE="Interactive mode (<Ctrl-D> to end): "

        while read -p "$MESSAGE" -r LINE; do
            MESSAGE=
            FORMULA="$FORMULA $LINE"
        done
    fi

    FORMULA=$(trim "$FORMULA")
    FORMULA=$(collapse "$FORMULA")

    if [ ! "$FORMULA" ]; then
        echo "No input formula." >&2
        exit 1
    fi

    TMPDIR=/tmp/me/mneri/pnglatex

    if [ ! -d $TMPDIR ]; then
        mkdir -p $TMPDIR
    fi

    TEXFILE=$(tempfile -d $TMPDIR -p f -s .tex)

    if [ "$ENVIRONMENT" = '$' ] || [ "$ENVIRONMENT" = '$$' ]; then
        BEGINENV=$ENVIRONMENT
        ENDENV=$ENVIRONMENT
    else
        BEGINENV="\begin{$ENVIRONMENT}"
        ENDENV="\end{$ENVIRONMENT}"
    fi

    PREFIX="\documentclass[${SIZE}pt]{article}$PACKAGES\pagestyle{empty}$HEADER\begin{document}$BEGINENV"
    SUFFIX="$ENDENV\end{document}"

    echo "$PREFIX $FORMULA $SUFFIX" > $TEXFILE
    latex -halt-on-error -interaction=nonstopmode -output-directory=$TMPDIR $TEXFILE | sed -n '/^!/,/^ /p' >&2

    if [ ${PIPESTATUS[0]} -ne 0 ]; then
        clean
        exit 1
    fi

    if [ ! "$PNGFILE" ]; then
        BNAME=$(basename $TEXFILE)
        PNGFILE=$PWD/${BNAME%.tex}.png
    fi

    if [ ! "$DPI" ]; then
        DPI=$(dpi)
    fi

    dvipng -bg $BACKGROUND -D $DPI -fg $FOREGROUND -o $PNGFILE -q --strict -T tight ${TEXFILE%.tex}.dvi > /dev/null

    if [ "$PADDING" ] || [ "$BORDER" ] || [ "$MARGIN" ]; then
        box
    fi

    if [ $OPTIMIZE -eq 1 ]; then
        optimize
    fi

    if [ $META -eq 1 ]; then
        meta
    fi

    if [ $SILENT -eq 0 ]; then
        readlink -f $PNGFILE
    fi

    clean
}

function main {
    if ! exists latex || ! exists dvipng; then
        echo "pnglatex requires latex and dvipng packages." >&2
        exit 1
    fi

    parse "$@"

    if [ $HELP -eq 1 ]; then
        usage
        exit 0
    fi

    if [ $SHOWVERSION -eq 1 ]; then
        version
        exit 0
    fi

    if [ "$FORMULA" ] && [ "$REVERSE" ]; then
        echo "Incompatible options: use either -f or -r, not both." >&2
        exit 1
    fi

    if [ "$REVERSE" ]; then
        if ! exists identify; then
            echo "Getting formula from image requires imagemagick package." >&2
            exit 1
        fi
    fi

    if [ "$PADDING" ] || [ "$BORDER" ] || [ "$MARGIN" ]; then
        if ! exists convert; then
            echo "Paddings, borders and margins require imagemagick package." >&2
            exit 1
        fi
    fi

    if [ $OPTIMIZE -eq 1 ]; then
        if ! exists optipng; then
            echo "Optimization requires optipng package." >&2
            exit 1
        fi
    fi

    if [ "$REVERSE" ]; then
        reverse
    else
        generate
    fi
}

function match {
    local PATTERN=$2
    local TEXT=$1

    return $(echo $TEXT | egrep $PATTERN &> /dev/null);
}

function meta {
    if exists convert; then
        convert -set latex:formula "${FORMULA//\\/\\\\}" -set generator "pnglatex $VERSION" $PNGFILE $PNGFILE
    fi
}

function optimize {
    optipng -f0-5 -quiet -zc1-9 -zm1-9 -zs0-3 $PNGFILE
}

function parse {
    while getopts b:B:d:e:f:F:hH:m:Mo:Op:P:r:s:Sv ARG; do
        case $ARG in
            b)
                BACKGROUND=$OPTARG
                ;;
            B)
                BORDER=$OPTARG
                ;;
            d)
                DPI=$OPTARG

                if ! match $DPI '^[1-9][0-9]*$'; then
                    echo "Invalid dpi." >&2
                    exit 1
                fi
                ;;
            e)
                ENVIRONMENT=$OPTARG
                ;;
            f)
                FORMULA=$OPTARG
                ;;
            F)
                FOREGROUND=$OPTARG
                ;;
            h)
                HELP=1
                ;;
            H)
                HEADER=\\input\{$OPTARG\}
                ;;
            m)
                MARGIN=$OPTARG

                if ! match $MARGIN '^[0-9]+(x[0-9]+)?$'; then
                    echo "Invalid margin." >&2
                    exit 1
                fi
                ;;
            M)
                META=0
                ;;
            o)
                PNGFILE=$OPTARG
                ;;
            O)
                OPTIMIZE=1
                ;;
            p)
                OIFS=$IFS
                IFS=":"
                PLIST=($OPTARG)

                for P in $PLIST; do
                    PACKAGES=$PACKAGES\\usepackage{$P}
                done

                IFS=$OIFS
                ;;
            P)
                PADDING=$OPTARG

                if ! match $PADDING '^[0-9]+(x[0-9]+)?$'; then
                    echo "Invalid padding." >&2
                    exit 1
                fi
                ;;
            r)
                REVERSE=$OPTARG
                ;;
            s)
                SIZE=$(echo $OPTARG | sed 's/pt//')

                if ! match $SIZE '^[1-9][0-9]*$'; then
                    echo "Invalid font size." >&2
                    exit 1
                fi
                ;;
            S)
                SILENT=1
                ;;
            v)
                SHOWVERSION=1
                ;;
            ?)
                exit 1
        esac
    done
}

function reverse {
    if [ ! -e "$REVERSE" ] || [ ! -r "$REVERSE" ]; then
        echo "Can't open file." >&2
        exit 1
    fi

    echo $(identify -verbose "$REVERSE" | grep 'latex:formula:' | sed -r 's/[ \t]*latex:formula:[ \t]*(.*)/\1/')
}

function scale {
    local BASEDPI=$(dpi)
    local WIDTH=$(($(echo $1 | sed -r 's/x.*//') * $DPI / $BASEDPI))
    local HEIGHT=$(($(echo $1 | sed -r 's/.*x//') * $DPI / $BASEDPI))

    echo ${WIDTH}x${HEIGHT}
}

function trim {
    echo $1 | sed -e 's/^[[:space:]]+//' -e 's/[[:space:]]+$//'
}

function usage {
    echo "pnglatex $VERSION - Write LaTeX formulas into PNG files."
    echo "Copyright Massimo Neri <hello@mneri.me>."
    echo
    echo "List of options:"
    echo "  -b <color>       Set the background color."
    echo "  -B <color>       Set the border color."
    echo "  -d <dpi>         Set the output resolution to the specified dpi."
    echo "  -e <environment> Set the environment."
    echo "  -f <formula>     The LaTeX formula."
    echo "  -F <color>       Set the foreground color."
    echo "  -h               Print this help message."
    echo "  -H <header>      Input file in header."
    echo "  -m <margin>      Set the margin."
    echo "  -M               Strip meta information."
    echo "  -o <file>        Specify the output file name."
    echo "  -O               Optimize the image."
    echo "  -p <packages>    A colon separated list of LaTeX package names."
    echo "  -P <padding>     Set the padding."
    echo "  -r <file>        Read an image and print the LaTeX formula."
    echo "  -s <size>        Set the font size."
    echo "  -S               Don't print image file name."
    echo "  -v               Show version."
    echo
    echo "Examples:"
    echo "  pnglatex -f \"E=mc^2\""
    echo "  pnglatex -e displaymath -f \"\sum_{i=0}^n i=\frac{n(n+1)}{2}\""
    echo "  pnglatex -b Transparent -p amssymb:amsmath -P 5x5 -s 12 -f \"A\triangleq B\""
}

function version {
    echo $VERSION
}

trap "clean" SIGINT SIGTERM
main "$@"

Оффлайн Александр Ерещенко

  • Завсегдатай
  • *
  • Сообщений: 1 153
А какая конечная цель использования графического представления формул? Может, найдется другой метод решения конечной задачи?
Хочу в программе отображать красиво введённые формулы, а изобретать велосипед не хочется.
Поищите по ключевому слову asciimath. (http://asciimath.org/)Там в основе  JavaScript и ориентирован на показ в браузерах (за счет их движка). Но есть библиотеки для php и пр.

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
А какая конечная цель использования графического представления формул? Может, найдется другой метод решения конечной задачи?
Хочу в программе отображать красиво введённые формулы, а изобретать велосипед не хочется.
Поищите по ключевому слову asciimath. (http://asciimath.org/)Там в основе  JavaScript и ориентирован на показ в браузерах (за счет их движка). Но есть библиотеки для php и пр.
Это немного не то, что мне нужно.

По тому что я накопал:
На баше запинается об команду tempfile. Мой баш такой команды не знает и синаптик тоже...  :-(
Компилированная программа на C++ делает то, что нужно. Но я посмотрел код, и вижу, что оба кода в принципе заставляют работать latex. И там написано какие нужны команды :-)
« Последнее редактирование: 20.02.2017 22:17:16 от tema »

Оффлайн tema

  • alt linux team
  • ***
  • Сообщений: 2 073
    • Email
В итоге остановился на:
klatexformula -X 200 --latexinput 'a^2+b^2=c^2' --output filename.png