为什么latex导入matlab代码是显示Missing $ inserted. %Position = [Position_?

发布于 2024-09-09 17:27:40

请问为什么我设置lstlisting插入其他matlab代码没有问题,但是在lstlisting环境插入是注释掉的matlab矩阵代码就有问题
36ab0d06666ab5c925ced32e636d1409.png
eab915b38b9a4e4fcc850157259a6e04.png
533cf8766758cb83af583ae5d4cd8e92.png

查看更多

关注者
0
被浏览
145
2 个回答
LaTeXStudio
LaTeXStudio 2024-09-09
这家伙很懒,什么也没写!

能否提供下完整的代码,用 ` ` 包起来。

鱼香肉丝没有鱼先生
鱼香肉丝没有鱼先生.

去掉

texcl = true
\documentclass[a4paper]{article} %确定是A4纸格式
\usepackage{geometry}
\geometry{a4paper,left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
%页边距左、右、上、下
\usepackage[UTF8,heading=true]{ctex}  %导入中文包

%%%%%%%%页眉页脚的处理%%%%%%%%%%%%
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt} % 移除页眉横线
\renewcommand{\footrulewidth}{0pt} % 移除页脚横线
\lfoot{}%这条语句可以让页码出现在下方
%%%%%%%%页眉页脚的处理%%%%%%%%%%%%

\usepackage{abstract} %导入摘要页
\renewcommand{\abstractname}{\bf\songti\zihao{4} 摘\hspace{1cm}要}%设置“摘要”两字的大小与间距
\setlength{\absleftindent} {1pt} %摘要的右缩进
\setlength{\absrightindent}{1pt} %摘要的左缩进

%调用数学公式包
\usepackage{amssymb}
\usepackage{amsmath}


\usepackage{listings}%用于设置插入附录的代码
\usepackage[dvipsnames]{xcolor} % 更全的色系
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% listings设置
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lstset{
    language = Matlab,
    backgroundcolor = \color{gray!10}, % 背景色:灰色
    basicstyle = \normalsize\ttfamily, % 基本样式 + 小号字体
    rulesepcolor= \color{blue!30}, % 代码块边框颜色
    breaklines = true, % 代码过长则换行
    flexiblecolumns,
    numbers = left, % 行号在左侧显示
    numberstyle = \small, % 行号字体
    keywordstyle = \color{blue}, % 关键字颜色
    commentstyle =\color{black!40!green}, % 注释颜色
    stringstyle = \color{red!100}, % 字符串颜色
    frame = shadowbox,  % 用(带影子效果)方框框住代码块
    showspaces = false, % 不显示空格
    columns = fixed, % 字间距固定
    % texcl=true,
    escapeinside=``,
}


%改变公式大小但不改变公式标号大小的设置%
\makeatletter
\renewcommand{\maketag@@@}[1]{\hbox{\m@th\normalsize\normalfont#1}}%
\makeatother
%改变公式大小但不改变公式标号大小的设置%

%自定义标题设置
%正文部分是小四,一级标题是三号,二级标题是小三,三级标题是四号
\ctexset{
    section = {
        format = \centering\bf\songti\zihao{3}, % 设置一级标题的字体格式
        name = {}, % 通常章节标题前不需要标点
        number = \chinese{section}, % 中文编号
        aftername = 、, % 章节标题后通常使用点号
    },
    subsection = {
        format = \bf\songti\zihao{4}, % 设置二级标题的字体格式
        name = {}, 
        aftername = 、,
    },
    subsubsection = {
        format = \bf\songti\zihao{-4}, % 设置三级标题的字体格式
        aftername = 、, 
    }
}
\fancyhead{} % 清除所有页眉内容

\usepackage{enumitem} 

%.svg图片
\usepackage{svg}

%表格调整
\usepackage{tabularx}

%表格加粗
\usepackage{booktabs}

%设置表格间距
\usepackage{caption}

%允许表格长跨页
\usepackage{longtable}

%调用浮动包
\usepackage{float}
\usepackage{subfig}

%调用表格
\usepackage{multirow}

\captionsetup[table]{labelsep=space} %去除表格标题的冒号

\usepackage{times}

\usepackage{graphicx}
\graphicspath{{figures/}} %图片在当前目录下的figures目录

\linespread{1.25} %行间距
\usepackage{multirow} % 用于合并行
\begin{document}
\begin{lstlisting}[language = Matlab]
clc;
close all;
clear;

load('Position_Longshen.mat')
load('Position_Longtou.mat')
load('Position_Longtou_Hou.mat')
load('data.mat')
%Position = [Position_Longtou;Position_Longtou_Hou;Position_Longshen];
Position = data;
% plot(Position(1:2:end,end),Position(2:2:end,end),'g');
% axis equal;
% hold on;

%求矩形的左下点
syms x1 y1 x2 y2;
Qian_Point = zeros(420,2);

for i = 1:420
% 定义方程
eq1 = (Position(1,i)-x1)^2 + (Position(2,i)-y1)^2 == 0.275^2 + 0.15^2;
eq2 = (Position(3,i)-x1)^2 + (Position(4,i)-y1)^2 == (3.41-0.275)^2 + 0.15^2;
eq3 = (Position(1,i)-x2)^2 + (Position(2,i)-y2)^2 == (3.41-0.275)^2 + 0.15^2;
eq4 = (Position(3,i)-x2)^2 + (Position(4,i)-y2)^2 == 0.275^2 + 0.15^2;
% 求解方程
sol1 = solve([eq1, eq2], [x1,y1]);
sol2 = solve([eq3,eq4], [x2,y2]);

x_decimal1 = double(sol1.x1);
y_decimal1 = double(sol1.y1);
x_decimal2 = double(sol2.x2);
y_decimal2 = double(sol2.y2);

vertices = [x_decimal1(2), y_decimal1(2);
x_decimal1(1), y_decimal1(1);
x_decimal2(1), y_decimal2(1);
x_decimal2(2), y_decimal2(2)];
Qian_Point(i,1) = x_decimal1(2);
Qian_Point(i,2) = y_decimal1(2);
% fill(vertices(:,1), vertices(:,2), 'r');
end
plot(Qian_Point(1:420,1)',Qian_Point(1:420,2)','g')
axis equal;

\end{lstlisting}
\end{document}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览