PHP Source Code Latex

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Hello can somebody please show the correct way to display my source code in Latex

The following code I want to display as php code

$id = addslashes($_GET['id']);

How can I display the above code in Latex as source code ? In My document .

I tried the below but get an error about a label not yet been referenced

\begin{lstlisting}
$id = addslashes($_GET['id']);
\label{phpheg}
\end{lstlisting}
profilepic.png
manpreet 2 years ago

As described in the documentation of the listings package, you have to define the caption and label in the options of the lstlisting environment. This is required because any text within lstlisting is typeset, so your code prints \label{phpheg} in the PDF, instead of calling that as a LaTeX command.

\documentclass{article}
\usepackage{listings}
\begin{document}
    See Listing~\ref{phpheg} for details.
\begin{lstlisting}[caption={PHP Code},label=phpheg]
$id = addslashes($_GET['id']);
\end{lstlisting}
    More text.
\end{document}

Note: as Werner says in his comment, as everything between \begin{lstlisting} and \end{lstlisting} is printed, thus indented code is also typeset indented. As a work-around, you can (and should) write the listing without any indention (as above).

As proposed by MaxNoe, you can use the lstautogobble package as a workaround: simply add \usepackage{autogobble} and set autogobble=true in the settings of lstlistings.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.