{"id":226778,"date":"2024-05-11T20:34:00","date_gmt":"2024-05-11T12:34:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/226778.html"},"modified":"2024-05-11T20:34:10","modified_gmt":"2024-05-11T12:34:10","slug":"latex%e4%b8%ad%e7%9a%84lstlisting%e5%a6%82%e4%bd%95%e5%bc%95%e7%94%a8%e6%9f%90%e5%8f%a5%e4%bb%a3%e7%a0%81%e6%89%80%e5%9c%a8%e7%9a%84%e8%a1%8c%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/226778.html","title":{"rendered":"LaTeX\u4e2d\u7684lstlisting\u5982\u4f55\u5f15\u7528\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25150122\/1b232cdd-3234-4462-9a07-2df0518c8e85.webp\" alt=\"LaTeX\u4e2d\u7684lstlisting\u5982\u4f55\u5f15\u7528\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7\" \/><\/p>\n<p><p>\u5728LaTeX\u4e2d\u4f7f\u7528lstlisting\u73af\u5883\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c55\u793a\u4ee3\u7801\uff0c\u800c\u8981\u5f15\u7528\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6elstlisting\u7684\u884c\u53f7\uff08numbers\uff09\u548c\u5f15\u7528\u6807\u8bb0\uff08escapeinside\uff09\u4e24\u4e2a\u9009\u9879<\/strong>\uff0c\u540c\u65f6\u5229\u7528LaTeX\u7684<code>\\label<\/code>\u548c<code>\\ref<\/code>\u547d\u4ee4\u3002\u5728\u5f15\u7528\u65f6\uff0c\u4f7f\u7528<code>\\ref<\/code>\u6807\u7b7e\u5373\u53ef\u5f15\u7528\u4ee3\u7801\u7684\u5177\u4f53\u884c\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u8bbe\u7f6elstlisting\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u662f\u8bbe\u7f6elstlisting\u73af\u5883\u4ee5\u663e\u793a\u884c\u53f7\u3002\u5728\u6587\u6863\u7684\u5bfc\u8a00\u533a\uff08\u5373<code>\\begin{document}<\/code>\u4e4b\u524d\uff09\u52a0\u8f7dlistings\u5b8f\u5305\uff0c\u5e76\u8fdb\u884c\u5fc5\u8981\u8bbe\u7f6e\uff0c\u6bd4\u5982\u663e\u793a\u884c\u53f7\uff08<code>numbers=left<\/code>\uff09\u4ee5\u53ca\u884c\u53f7\u7684\u6837\u5f0f\u3001\u95f4\u9694\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-latex\">\\usepackage{listings}<\/p>\n<p>\\lstset{<\/p>\n<p>  numbers=left,<\/p>\n<p>  numberstyle=\\tiny,<\/p>\n<p>  numbersep=5pt,<\/p>\n<p>  frame=tb,<\/p>\n<p>  captionpos=b,<\/p>\n<p>  escapeinside={(*@}{@*)},<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u8bbe\u7f6e\u4e86\u884c\u53f7\u663e\u793a\u5728\u5de6\u4fa7\u3001\u884c\u53f7\u6837\u5f0f\u3001\u4e0e\u4ee3\u7801\u7684\u95f4\u9694\u4ee5\u53ca\u5f15\u7528\u6807\u8bb0\u4e3a<code>(*@<\/code>\u548c<code>@*)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528lstlisting\u663e\u793a\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u6863\u4e2d\u4f7f\u7528lstlisting\u73af\u5883\u5c55\u793a\u4ee3\u7801\uff0c\u5e76\u5728\u9700\u8981\u5f15\u7528\u7684\u4ee3\u7801\u884c\u540e\u9762\u6dfb\u52a0\u6807\u7b7e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-latex\">\\begin{lstlisting}<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    printf(&quot;Hello, World!&quot;); (*@\\label{line:hello}@*)<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p>\\end{lstlisting}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5728\u201cHello, World!\u201d\u8fd9\u4e00\u884c\u4ee3\u7801\u540e\u6dfb\u52a0\u4e86<code>\\label{line:hello}<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5f15\u7528\u4ee3\u7801\u884c\u53f7<\/h3>\n<\/p>\n<p><p>\u5728\u6b63\u6587\u4e2d\uff0c\u5f53\u9700\u8981\u5f15\u7528\u4e4b\u524d\u6807\u8bb0\u7684\u90a3\u4e00\u884c\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>\\ref<\/code>\u547d\u4ee4\u5f15\u7528\u884c\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-latex\">As shown on line \\ref{line:hello} of Listing \\ref{lst:example}, the `printf` function is used to print a string to the console.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u6587\u672c\u4e2d\u5f15\u7528\u4ee3\u7801\u884c\u53f7\u4e86\u3002\u5f53\u6587\u6863\u7f16\u8bd1\u540e\uff0c<code>\\ref{line:hello}<\/code>\u4f1a\u88ab\u66ff\u6362\u6210\u5bf9\u5e94\u7684\u884c\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u8bd1\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u5f15\u7528\u6b63\u786e\u663e\u793a\uff0c\u901a\u5e38\u9700\u8981\u5bf9LaTeX\u6587\u6863\u8fdb\u884c\u591a\u6b21\u7f16\u8bd1\u3002\u9996\u6b21\u7f16\u8bd1\u65f6\uff0cLaTeX\u4f1a\u5728\u8f85\u52a9\u6587\u4ef6\u4e2d\u8bb0\u5f55\u4e0b\u6807\u7b7e\u548c\u884c\u53f7\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u7b2c\u4e8c\u6b21\u7f16\u8bd1\u65f6\u5219\u4f1a\u5728\u6b63\u6587\u4e2d\u6b63\u786e\u663e\u793a\u5f15\u7528\u7684\u884c\u53f7\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728LaTeX\u6587\u6863\u4e2d\u9ad8\u6548\u5730\u5f15\u7528lstlisting\u73af\u5883\u4e2d\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7\u3002\u8fd9\u4e00\u529f\u80fd\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u7f16\u5199\u6d89\u53ca\u5177\u4f53\u4ee3\u7801\u5206\u6790\u7684\u6280\u672f\u6587\u6863\u65f6\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u8bfb\u8005\u80fd\u591f\u76f4\u63a5\u770b\u5230\u5f15\u7528\u7684\u4e0a\u4e0b\u6587\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u529f\u80fd\u548c\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728LaTeX\u7684lstlisting\u73af\u5883\u4e2d\u663e\u793a\u4ee3\u7801\u884c\u53f7\uff1f<\/strong><\/p>\n<p>\u5728LaTeX\u7684lstlisting\u73af\u5883\u4e2d\u9ed8\u8ba4\u662f\u4e0d\u663e\u793a\u4ee3\u7801\u884c\u53f7\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6765\u5b9e\u73b0\u663e\u793a\u884c\u53f7\u3002\u53ef\u4ee5\u4f7f\u7528lstset\u547d\u4ee4\u6765\u914d\u7f6elstlisting\u73af\u5883\uff0c\u5c06numbers\u5c5e\u6027\u8bbe\u7f6e\u4e3atrue\uff0c\u5373\u53ef\u663e\u793a\u884c\u53f7\u3002\u4f8b\u5982\uff1a\\lstset{numbers=left}\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5f15\u7528LaTeX\u4e2dlstlisting\u73af\u5883\u4e2d\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7\uff1f<\/strong><\/p>\n<p>\u5728LaTeX\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lstlisting\u73af\u5883\u4e2d\u7684label\u5c5e\u6027\u6765\u7ed9\u67d0\u53e5\u4ee3\u7801\u6dfb\u52a0\u6807\u7b7e\uff0c\u7136\u540e\u4f7f\u7528ref\u547d\u4ee4\u6765\u5f15\u7528\u8be5\u6807\u7b7e\u6240\u5728\u7684\u884c\u53f7\u3002\u9996\u5148\uff0c\u5728\u9700\u8981\u6807\u8bb0\u7684\u884c\u6dfb\u52a0label\u5c5e\u6027\uff0c\u5982\\lstset{escapechar=&amp;}\\begin{lstlisting}[label=code,label=lst:test]code that needs referencing\\end{lstlisting}\u3002\u7136\u540e\uff0c\u5728\u60f3\u8981\u5f15\u7528\u8fd9\u4e2a\u884c\u53f7\u7684\u5730\u65b9\u4f7f\u7528ref\u547d\u4ee4\uff0c\u5982\u5728\u6587\u672c\u4e2d\u4f7f\u7528\\ref{lst:test}\u6765\u5f15\u7528\u8be5\u884c\u53f7\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u81ea\u5b9a\u4e49LaTeX\u4e2dlstlisting\u73af\u5883\u4ee3\u7801\u884c\u53f7\u7684\u6837\u5f0f\uff1f<\/strong><\/p>\n<p>LaTeX\u4e2d\u7684lstlisting\u73af\u5883\u4e2d\u7684\u4ee3\u7801\u884c\u53f7\u6837\u5f0f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6elstset\u547d\u4ee4\u4e2d\u7684numberstyle\u5c5e\u6027\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002numberstyle\u5c5e\u6027\u53ef\u4ee5\u63a5\u53d7\u5404\u79cdLaTeX\u547d\u4ee4\u548c\u683c\u5f0f\uff0c\u6bd4\u5982\u8bbe\u7f6e\u6210\\small\\color{gray}\u53ef\u4ee5\u8ba9\u884c\u53f7\u53d8\u5c0f\u4e14\u989c\u8272\u53d8\u4e3a\u7070\u8272\u3002\u4f8b\u5982\uff1a\\lstset{numbers=left,numberstyle=\\small\\color{gray}}\u3002\u901a\u8fc7\u8c03\u6574numberstyle\u5c5e\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6539\u53d8\u4ee3\u7801\u884c\u53f7\u7684\u6837\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728LaTeX\u4e2d\u4f7f\u7528lstlisting\u73af\u5883\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c55\u793a\u4ee3\u7801\uff0c\u800c\u8981\u5f15\u7528\u67d0\u53e5\u4ee3\u7801\u6240\u5728\u7684\u884c\u53f7\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6elst [&hellip;]","protected":false},"author":3,"featured_media":226796,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/226778"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=226778"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/226778\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/226796"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=226778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=226778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=226778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}