# latex2code **Repository Path**: wzxii/latex2code ## Basic Information - **Project Name**: latex2code - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-29 - **Last Updated**: 2026-01-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Latex2Code ### Introduction This project aims to convert pseudo code to c code segment. It can deal with pseudo codes with complex math expressions and matrix operations, as well as translate the variables in the pseudo code to the relavent global/temporary variables in the c code. To do this, you should prepare two config files in advance, the first of which records the global and temporary variables, function declarations and some constants while the second one records the matrix calculation functions. ### Pseudo Code Grammar To implement the convertion, you should write the pseudo code in the given grammar. 1. Every statement should end with a ';', just like statements in c codes. For examples: ```c a = b; x = Func(y); ``` 2. For the if-statement,you should format the style like this: ``` If1 (a < b) { a = 1; } Elseif1 (a == b) { a = 2; } Else1 { If2 (b > 1) a = 3; Else2 a = 4; Endif2 } Endif1 ``` You should use the specific keyword:'Ifn, Elseifn, Elsen, Endifn', where 'n' means the nesting layer of the condition statements, and there must be a 'Endifn' at the end of every if-statement. If there is only one sentence of the given condition, '{}' can be ignored. 1. Our pseudo code grammar gives expressions of math symbols and most of the expressions are compatible with LaTeX grammars. (Except the piecewise function). ### Supported LATEX symbols | Symbol | Format | Description | Markdown | | :-----: | :-----------------: | :---------------: | :-------------------: | | \frac | \frac{expr1}{expr2} | fraction | $\frac{expr1}{expr2}$ | | + | expr1 + expr2 | add or plus sign | $expr1 + expr2$ | | - | expr1 - expr2 | sub or minus sign | $expr1 - expr2$ | | * | expr1 * expr2 | mul | $expr1 * expr2$ | | \ | expr1 \ expr2 | div | $expr1 \ expr2$ | | \times | expr1 \times expr2 | mul | $expr1 \times expr2$ | | \cdot | expr1\cdot expr2 | mul | $expr1\cdot expr2$ | | \div | expr1 \div expr2 | div | $expr1 \div expr2$ | | \sqrt | \sqrt{expr} | square root | $\sqrt{expr}$ | | ^ | {expr1}^{expr2} | power | ${expr1}^{expr2}$ | | \|\| | \|expr\| | abs | $\|expr\|$ | * note: the LATEX math expression must be used strictly based on the format in the table above * For piecewise function, we use a specific format, and it's not compatible with LaTeX. For example: ``` a \eqmulti{1@x=0,2@x=1,3@x=2}; ``` * For absolute value expression, we use a pair of '|' outside the value to express it. However, the symbols can not be nested. E.g. expression like |x + |y|| is not permitted. And for one-dimension vector, you can express it in the following way, and all elements inside the square brackets should be scalars: ``` [x[0] x[1] x[2]] ``` Using our tool to translate it to c code: ```c if (x == 0) { a = 1; } else if (x == 1) { a = 2; } else if (x == 2) { a = 3; } ``` ### Configs Style Configs are writen using json files which record names, dims, types of variables and declarations of functions. 1.Variables and Functions Config Variables and unctions config records global variable dictionary, function dictionary, constant dictionary, temporary variable dictionary and functional description of the code segment in order. You can write the config as the following example(config of case 2 in '20220930.算法中公式表示V1.03'): ```json [ { "FP_InertialPsi0Dir_Set": { "name": "sAttDeter.bPermitPsi0DirSet", "type": "unint32", "dim": "1*1" }, "\\Delta\\varphi_{m0}": { "name": "sAttDeter.angleTargetSet[0]", "type": "float64", "dim": "1*1" }, "\\Delta\\theta_{m0}": { "name": "sAttDeter.angleTargetSet[1]", "type": "float64", "dim": "1*1" }, "F_InertialPsi0Dir": { "name": "sAttDeter.flgInertialPsi0Dir", "type": "unint32", "dim": "1*1" }, "\\Delta\\psi_{m0}": { "name": "sAttDeter.angleTargetSet[2]", "type": "float64", "dim": "1*1" }, "FP_XToEarthDir": { "name": "sAttDeter.bPermitXtoEarth", "type": "unint32", "dim": "1*1" }, "\\varphi_{m0}": { "name": "sAttDeter.anglem0[0]", "type": "float64", "dim": "1*1" }, "F_TargetAtt": { "name": "sAttDeter.flgTargetAtt", "type": "unint32", "dim": "1*1" }, "\\theta_{m0}": { "name": "sAttDeter.anglem0[1]", "type": "float64", "dim": "1*1" }, "\\psi_{m0}": { "name": "sAttDeter.anglem0[2]", "type": "float64", "dim": "1*1" }, "C_{m0}": { "name": "sAttDeter.cm0", "type": "float64", "dim": "3*3" }, "q_{m0}": { "name": "sAttDeter.qm0[4]", "type": "float64", "dim": "4*1" } }, { "dcm2q": { "declaration" : "void C2Q(float64 *pOutQ, const float64 *pMat)", "output_type" : "float64", "output_dim" : "4*1" }, "mod": { "declaration" : "float64 ModPNHP(float64 x, float64 halfperiod)", "output_type" : "float64", "output_dim" : "1*1" }, "dcm312": { "declaration" : "void Angle2C312(float64 *pOutMA, float64 bi, float64 bj, float64 bk, unint32 ijkHex)", "output_type" : "float64", "output_dim" : "3*3" }, "dcm231": { "declaration" : "void Angle2C231(float64 *mat, float64 x, float64 y, float64 z, unint32 ijkHex)", "output_type" : "float64", "output_dim" : "3*3" }, "Rx": { "declaration" : "void Rx(float64 *pOutMA, float64 radianX)", "output_type" : "float64", "output_dim" : "3*3" }, "Ry": { "declaration" : "void Ry(float64 *pOutMA, float64 radianY)", "output_type" : "float64", "output_dim" : "3*3" }, "Rz": { "declaration" : "void Rz(float64 *pOutMA, float64 radianZ)", "output_type" : "float64", "output_dim" : "3*3" } }, { "\\pi": "PI" }, { "C_{tmp}": { "type": "float64", "dim": "3*3" } }, "姿态机动目标姿态确定的初始化" ] ``` 2.Matrix Functions Config Matrix Functions Config records the matrix function library, including matrix multiplication, matrix add, matrix transposition and matrix assignment in order. When parsing the symbols in the psedo code, the parser will distinguish scalars and matrice respectively and generate c code using functions in Matrix Functions Config.You can write the config as the following example(all matrix functions in '20220930.算法中公式表示V1.03'): ```json { "multiply" : { "MatrixMulti333" : { "declaration" : "void MatrixMulti333(float64 *pOut, const float64 *pfad, const float64 *pMuler)", "input_dim" : "3*3*3", "input_type" : "float64", "output_dim" : "3*3", "output_type" : "float64" }, "MatrixMulti331" : { "declaration" : "void MatrixMulti331(float64 *pOut, const float64 *pfad, const float64 *pMuler)", "input_dim" : "3*3*1", "input_type" : "float64", "output_dim" : "3*1", "output_type" : "float64" } }, "add" : { "VectorAdd3" : { "declaration" : "void VectorAdd3(float64 *pOut, const float64 *pV1, const float64 *pV2)", "dim" : "1*3", "type" : "float64" } }, "transpose" : { "MatrixTran33" : { "declaration" : "void MatrixTran33(float64 *matrixT, float64 *matrixRaw)", "input_dim" : "3*3", "input_type" : "float64", "output_dim" : "3*3", "output_type" : "float64" } }, "equal" : { "MatrixEval" : { "declaration" : "void MatrixEval(float64 *pOut, const float64 *pInM, siint32 nrow, siint32 ncol)", "type" : "float64" } } } ``` ### Example With the 2 configs and the pseudo code writen in given grammars, you can now generate the c code. You need to put the Matrix Functions Config as the following path in the current folder: './configs/matrix_config.json', and you can place the Variables and Functions Config to anywhere and set the path like the following code snippet('./configs/config_test.json'). Config file: ```json [ { "d": { "name": "dd.cc", "type": "float64", "dim": "3*3" }, "h": { "name": "h.bb", "type": "float64", "dim": "3*3" }, "j": { "name": "j.bb", "type": "float64", "dim": "3*3" } }, { "func": { "declaration" : "void Func(float64 y, float64 x)", "output_type" : "float64", "output_dim" : "3*3" } }, { }, { "aa" : { "type" : "float64", "dim" : "1*1" }, "bb" : { "type" : "float64", "dim" : "1*1" }, "cc" : { "type" : "float64", "dim" : "1*1" } }, "测试代码" ] ``` Python parse code: ```python from utils.code_parser import Parser # pseudo code writen in the given grammar code = r''' a \eqmulti{1&x=0,2&x=1,3&x=2}; b = c*func(a); c={d}^{T}; c = d*h*j; x=[d[0] d[1] d[2]]; ''' # you need to give the path of Variables and Functions Config parser = Parser(code, './configs/config_test.json') # implement of the parsing parser.parse() # print the c code generated parser.print_codes() ``` the output is: ```c double tmp0[3][3]; double tmp1[3][3]; double tmp2[3][3]; if (x == 0) { a = 1; } else if (x == 1) { a = 2; } else if (x == 2) { a = 3; } Func(tmp0, a); b = c * tmp0; MatrixTran33(c, dd.cc); MatrixMulti333(tmp2, h.bb, j.bb); MatrixMulti333(c, dd.cc, tmp2); x[0] = dd.cc[0]; x[1] = dd.cc[1]; x[2] = dd.cc[2]; ```