Format Code/PseudocodePut a grey background behind code extracts in a Latex document (like this site does)Pseudocode, special formattingAddition and subtraction of chemical equationsReplicate pseudocode formatPseudocode in LaTeXHow to format a pseudocode algorithmFormatting in Latex - PseudocodeHow to write pseudocode similar to code presented in “Beautiful Code” by J. R. HeardPseudocode vertical linesChange name in pseudocodeLatex formatting a pseudocode
CLI: Get information Ubuntu releases
Why are there no stars visible in cislunar space?
"Marked down as someone wanting to sell shares." What does that mean?
The English Debate
Homology of the fiber
What is it called when someone votes for an option that's not their first choice?
Why is "la Gestapo" feminine?
Nested Dynamic SOQL Query
Help with identifying unique aircraft over NE Pennsylvania
How do you justify more code being written by following clean code practices?
label a part of commutative diagram
Exit shell with shortcut (not typing exit) that closes session properly
Should a narrator ever describe things based on a characters view instead of fact?
Print last inputted byte
How to balance a monster modification (zombie)?
UK Tourist Visa- Enquiry
How to find the largest number(s) in a list of elements, possibly non-unique?
is this saw blade faulty?
Air travel with refrigerated insulin
Asserting that Atheism and Theism are both faith based positions
Is xar preinstalled on macOS?
Could any one tell what PN is this Chip? Thanks~
Hackerrank All Women's Codesprint 2019: Name the Product
TDE Master Key Rotation
Format Code/Pseudocode
Put a grey background behind code extracts in a Latex document (like this site does)Pseudocode, special formattingAddition and subtraction of chemical equationsReplicate pseudocode formatPseudocode in LaTeXHow to format a pseudocode algorithmFormatting in Latex - PseudocodeHow to write pseudocode similar to code presented in “Beautiful Code” by J. R. HeardPseudocode vertical linesChange name in pseudocodeLatex formatting a pseudocode
In a document containing a lot of other stuff, I have the following code which I have kept in verbatim(for lack of other ideas). As you can see, the alignment doesn't look so good.
How do I make it more presentable, so that it becomes more readable, with indentation perhaps(verbatim doesnt seem to handle spaces and tabs)?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
formatting pseudocode
|
show 5 more comments
In a document containing a lot of other stuff, I have the following code which I have kept in verbatim(for lack of other ideas). As you can see, the alignment doesn't look so good.
How do I make it more presentable, so that it becomes more readable, with indentation perhaps(verbatim doesnt seem to handle spaces and tabs)?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
formatting pseudocode
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
1
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
2
For the automatic indenting you need to call an external tool, as neitherlistings
orminted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?
– Marijn
Feb 26 at 12:51
|
show 5 more comments
In a document containing a lot of other stuff, I have the following code which I have kept in verbatim(for lack of other ideas). As you can see, the alignment doesn't look so good.
How do I make it more presentable, so that it becomes more readable, with indentation perhaps(verbatim doesnt seem to handle spaces and tabs)?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
formatting pseudocode
In a document containing a lot of other stuff, I have the following code which I have kept in verbatim(for lack of other ideas). As you can see, the alignment doesn't look so good.
How do I make it more presentable, so that it becomes more readable, with indentation perhaps(verbatim doesnt seem to handle spaces and tabs)?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
formatting pseudocode
formatting pseudocode
edited 5 mins ago
GermanShepherd
asked Feb 26 at 11:58
GermanShepherdGermanShepherd
545220
545220
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
1
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
2
For the automatic indenting you need to call an external tool, as neitherlistings
orminted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?
– Marijn
Feb 26 at 12:51
|
show 5 more comments
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
1
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
2
For the automatic indenting you need to call an external tool, as neitherlistings
orminted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?
– Marijn
Feb 26 at 12:51
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
1
1
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
2
2
For the automatic indenting you need to call an external tool, as neither
listings
or minted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?– Marijn
Feb 26 at 12:51
For the automatic indenting you need to call an external tool, as neither
listings
or minted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?– Marijn
Feb 26 at 12:51
|
show 5 more comments
3 Answers
3
active
oldest
votes
Something like this?
documentclassbook
usepackagelistings
lstdefinestylemyListingStyle
basicstyle = smallttfamily,
breaklines = true,
begindocument
beginlstlisting[
style = myListingStyle,
caption = Nice listing.
]
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and removecaption = Nice listing.
.
– Dr. Manuel Kuehner
Feb 26 at 12:36
add a comment |
Verbatim
Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
Listings
The listings
package can add colors.
documentclass[]article
usepackagelistings
usepackagexcolor
lstsetbasicstyle=ttfamily,language=c,keywordstyle=colorblue
begindocument
beginlstlisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
Minted
The minted
package is even more powerful…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
begindocument
beginmintedc
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endminted
enddocument
Tcblisting
You can of course combine it with tcolorbox or something else…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
usepackage[minted]tcolorbox
newtcblistingmylistinglisting only,listing engine=minted, minted language=c,colback=gray!20
begindocument
beginmylisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endmylisting
enddocument
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
add a comment |
As already stated by others, verbatim
does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim
is ignoring for some reason. So what I do is I put my code into verbatim
select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).
Are you sure thatverbatim
does not support tabs? In my case, it does.
– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f476745%2fformat-code-pseudocode%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this?
documentclassbook
usepackagelistings
lstdefinestylemyListingStyle
basicstyle = smallttfamily,
breaklines = true,
begindocument
beginlstlisting[
style = myListingStyle,
caption = Nice listing.
]
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and removecaption = Nice listing.
.
– Dr. Manuel Kuehner
Feb 26 at 12:36
add a comment |
Something like this?
documentclassbook
usepackagelistings
lstdefinestylemyListingStyle
basicstyle = smallttfamily,
breaklines = true,
begindocument
beginlstlisting[
style = myListingStyle,
caption = Nice listing.
]
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and removecaption = Nice listing.
.
– Dr. Manuel Kuehner
Feb 26 at 12:36
add a comment |
Something like this?
documentclassbook
usepackagelistings
lstdefinestylemyListingStyle
basicstyle = smallttfamily,
breaklines = true,
begindocument
beginlstlisting[
style = myListingStyle,
caption = Nice listing.
]
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.
Something like this?
documentclassbook
usepackagelistings
lstdefinestylemyListingStyle
basicstyle = smallttfamily,
breaklines = true,
begindocument
beginlstlisting[
style = myListingStyle,
caption = Nice listing.
]
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.
edited Feb 26 at 12:46
answered Feb 26 at 12:28
Dr. Manuel KuehnerDr. Manuel Kuehner
9,25932769
9,25932769
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and removecaption = Nice listing.
.
– Dr. Manuel Kuehner
Feb 26 at 12:36
add a comment |
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and removecaption = Nice listing.
.
– Dr. Manuel Kuehner
Feb 26 at 12:36
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
Is it possible to remove the caption for listings?
– GermanShepherd
Feb 26 at 12:35
@GermanShepherd Yes, just have a look at the code and remove
caption = Nice listing.
.– Dr. Manuel Kuehner
Feb 26 at 12:36
@GermanShepherd Yes, just have a look at the code and remove
caption = Nice listing.
.– Dr. Manuel Kuehner
Feb 26 at 12:36
add a comment |
Verbatim
Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
Listings
The listings
package can add colors.
documentclass[]article
usepackagelistings
usepackagexcolor
lstsetbasicstyle=ttfamily,language=c,keywordstyle=colorblue
begindocument
beginlstlisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
Minted
The minted
package is even more powerful…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
begindocument
beginmintedc
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endminted
enddocument
Tcblisting
You can of course combine it with tcolorbox or something else…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
usepackage[minted]tcolorbox
newtcblistingmylistinglisting only,listing engine=minted, minted language=c,colback=gray!20
begindocument
beginmylisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endmylisting
enddocument
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
add a comment |
Verbatim
Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
Listings
The listings
package can add colors.
documentclass[]article
usepackagelistings
usepackagexcolor
lstsetbasicstyle=ttfamily,language=c,keywordstyle=colorblue
begindocument
beginlstlisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
Minted
The minted
package is even more powerful…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
begindocument
beginmintedc
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endminted
enddocument
Tcblisting
You can of course combine it with tcolorbox or something else…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
usepackage[minted]tcolorbox
newtcblistingmylistinglisting only,listing engine=minted, minted language=c,colback=gray!20
begindocument
beginmylisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endmylisting
enddocument
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
add a comment |
Verbatim
Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
Listings
The listings
package can add colors.
documentclass[]article
usepackagelistings
usepackagexcolor
lstsetbasicstyle=ttfamily,language=c,keywordstyle=colorblue
begindocument
beginlstlisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
Minted
The minted
package is even more powerful…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
begindocument
beginmintedc
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endminted
enddocument
Tcblisting
You can of course combine it with tcolorbox or something else…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
usepackage[minted]tcolorbox
newtcblistingmylistinglisting only,listing engine=minted, minted language=c,colback=gray!20
begindocument
beginmylisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endmylisting
enddocument
Verbatim
Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?
documentclass[]article
begindocument
beginverbatim
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endverbatim
enddocument
Listings
The listings
package can add colors.
documentclass[]article
usepackagelistings
usepackagexcolor
lstsetbasicstyle=ttfamily,language=c,keywordstyle=colorblue
begindocument
beginlstlisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endlstlisting
enddocument
Minted
The minted
package is even more powerful…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
begindocument
beginmintedc
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endminted
enddocument
Tcblisting
You can of course combine it with tcolorbox or something else…
% arara: pdflatex: shell: yes
documentclass[]article
usepackageminted
usepackage[minted]tcolorbox
newtcblistingmylistinglisting only,listing engine=minted, minted language=c,colback=gray!20
begindocument
beginmylisting
if (n == 0 || n == 1)
return n;
j = 0;
for (i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j] = arr[i];
j++;
arr[j++] = arr[n-1];
endmylisting
enddocument
edited Feb 26 at 12:36
answered Feb 26 at 12:29
TeXnicianTeXnician
25.7k63390
25.7k63390
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
add a comment |
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
1
1
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
+1: Very complete answer :)
– Dr. Manuel Kuehner
Feb 26 at 12:36
2
2
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
@Dr.ManuelKuehner By far not, but listing some common options :)
– TeXnician
Feb 26 at 12:37
add a comment |
As already stated by others, verbatim
does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim
is ignoring for some reason. So what I do is I put my code into verbatim
select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).
Are you sure thatverbatim
does not support tabs? In my case, it does.
– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
add a comment |
As already stated by others, verbatim
does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim
is ignoring for some reason. So what I do is I put my code into verbatim
select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).
Are you sure thatverbatim
does not support tabs? In my case, it does.
– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
add a comment |
As already stated by others, verbatim
does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim
is ignoring for some reason. So what I do is I put my code into verbatim
select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).
As already stated by others, verbatim
does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim
is ignoring for some reason. So what I do is I put my code into verbatim
select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).
edited Feb 26 at 16:13
JouleV
6,46121750
6,46121750
answered Feb 26 at 16:09
der benderder bender
679
679
Are you sure thatverbatim
does not support tabs? In my case, it does.
– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
add a comment |
Are you sure thatverbatim
does not support tabs? In my case, it does.
– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
Are you sure that
verbatim
does not support tabs? In my case, it does.– JouleV
Feb 26 at 16:16
Are you sure that
verbatim
does not support tabs? In my case, it does.– JouleV
Feb 26 at 16:16
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however.
– der bender
Feb 26 at 16:18
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f476745%2fformat-code-pseudocode%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "" on it).
– DG'
Feb 26 at 12:01
@DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange.
– GermanShepherd
Feb 26 at 12:13
Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those?
– TeXnician
Feb 26 at 12:13
1
Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong.
– TeXnician
Feb 26 at 12:23
2
For the automatic indenting you need to call an external tool, as neither
listings
orminted
provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use?– Marijn
Feb 26 at 12:51