2

So there are paragraphs in my document, and even if I try to comment a single line using % I find that all the rest of the paragraph is also commented. I know this since the color changes to grey (using TeX Studio 2.5.2). What is the solution? I don't think I need the comment package.

  • 4
    A % comment character tells TeX to skip everything from that until the next "newline" character (\n in C programming notation). So if your paragraph is written on a single line, without any hard returns, then the comment character will cause it all to be ignored. Many of us use a new line for each sentence in TeX documents. – musarithmia May 29 '15 at 21:27
  • 1
    But we would need a small example document that we could test in order to know for sure. – musarithmia May 29 '15 at 21:27
  • 2
    You probably have the paragraph types on a single line, yet TeX Studio wraps the lines to the screen. You can insert manual line-breaks (without leaving a blank line) and it would allow you to comment out specific "lines" in a regular paragraph. – Werner May 29 '15 at 21:43
  • 1
    IMO, Opt.2 from @cptnjtk's answer should suite your requirements. However, please see my comment to that answer before using Opt.2. I use such a command often in my work to comment out specific lines/ blocks from paragraphs. – Amar May 30 '15 at 1:15
6

All of the comments under the question are correct. TeXstudio wraps lines that go beyond the width of the viewing window. This is what allows you to type a full paragraph on a single line, and when putting in comments, it is treated as a single line. Take a look at the line wrap example image below.

You can see here in TeXstudio that the first line is too long, and you get the wrap symbol.

One solution I often use is to put a newline after every sentence in a paragraph as follows:

\documentclass[10pt,a4paper]{article}
\begin{document}
    Tractography refers to the three-dimensional modeling technique for visually display neural tracts.
    %
    The source data for tractography comes from diffusion tensor imaging (DTI) and magnetic resonance imaging (MRI).
    %
    By employing analysis techniques (including image analysis) on this source data, neural tracts can be identified throughout the brain.
    %
    These neural tracts are encoded as a series of line segments.


    Tractography is used for multiple purposes.
    %
    First, surgeons use tractography in conjunction with anatomical knowledge to plan surgeries around critical motor neurons for actions such as speech or movement. 
    %
    While this usage often does not require real-time analysis, the timelines between
    acquiring data and analysis by the surgeon is typically on the order of days.
    %
    Neuroscience researchers use tractography data in a different way: to create maps of the human brain.
    %
    In this case, the analysis has no real-time component, and analysis techniques taking longer periods are acceptable.
\end{document}

Writing documents like this makes it very easy to add, edit, or remove sentences, and does not cause the comment issue that you are seeing.

Option 2 If you do not want to restructure your paragraphs as in the above example and must comment them in place, you could define a comment command: \newcommand{\comment}[1]{\ignorespaces}

This allows you to comment a single sentence in a block paragraph, when % would comment the entire remainder of the block. Notice how these paragraphs run off on one line; that is what was tripping TeXstudio up.

\documentclass[10pt,a4paper]{article}
\newcommand{\comment}[1]{\ignorespaces} % corrected!
\begin{document}    
    Tractography is used for multiple purposes.\comment{First, surgeons use tractography in conjunction with anatomical knowledge to plan surgeries around critical motor neurons for actions such as speech or movement.} While this usage often does not require real-time analysis, the timelines between acquiring data and analysis by the surgeon is typically on the order of days. Neuroscience researchers use tractography data in a different way: to create maps of the human brain. In this case, the analysis has no real-time component, and analysis techniques taking longer periods are acceptable.

    Tractography is used for multiple purposes. %First, surgeons use tractography in conjunction with anatomical knowledge to plan surgeries around critical motor neurons for actions such as speech or movement. While this usage often does not require real-time analysis, the timelines between acquiring data and analysis by the surgeon is typically on the order of days. Neuroscience researchers use tractography data in a different way: to create maps of the human brain. In this case, the analysis has no real-time component, and analysis techniques taking longer periods are acceptable.

\end{document}

Option 3 This option will allow you to have comment style highlighting around the new comment environment. The environment declaration is: \newenvironment{hashed}[1]{\ignorespaces}{\ignorespacesafterend}

To enable hightlighting in TeXstudio go to "Options->Configure Texstudio..." and add the command "hashed" to "Custom Highlighting" and set the "Type of Environment" to "comment" as follows: enter image description here

It can be used as follows (notice the comment style highlighting): enter image description here

This may not be the cleanest method, but custom environemnts are the only way that I know to add custom highlighting to TeXstudio.

  • 1
    A correction to Opt.2: If you define \newcommand{\comment}[1]{} it would leave an extra space at the end. To take care of the additional spcace, either define it as \newcommand{\comment}[1]{\ignorespaces} or simply as \newcommand{\comment}[2]{#2}. You may compare the results from all 3 commands by commenting out a few words in the middle of a sentence. – Amar May 30 '15 at 1:18
  • You are correct about the space, if you put a space after the period before the comment command like: abc. \comment{...}. If there is not space though, you are good to go. Thanks for pointy this out though! – cptnjtk May 30 '15 at 6:55
  • Usually, as one types a paragraph, it is by habit that we put a <space> after ending any sentence with a period. I'd personally find it very odd to see a paragraph that runs continuously without any spaces separating the periods (for that matter, other punctuation marks like ; , : as well) and the start of a new sentence! Please note, that if you use this macro (as suggested above) within a line, it WILL add a trailing space! For more explanation, please see my answer given below. – Amar May 30 '15 at 7:18
  • While this is a correct solution, as I marked, it has the disadvantage that the commented block now doesn't have specific highlighting since TeX Studio expects comments to start with % and not with \comment. I looked into the highlighting section in the options and it doesn't seem that I can add my own. Any ideas? – student1 May 30 '15 at 21:07
  • Good point on the highlighting, this way of commenting does make visually parsing more difficult in TeXstudio. I have amended my answer with a third more involved option (not the cleanest but does work well), and it does let you have highlighting! Thanks to @Amar for the pointer on ignoring spaces. – cptnjtk May 30 '15 at 23:13
1

As @werner has already suggested (in his comment on your question), you can insert manual line-breaks (without leaving a blank line) and it would allow you to comment out specific lines in a regular paragraph.

Otherwise you can define a new macro which can selectively comment-out only the specific line or part of the targeted paragraph and if you use this method, you may continue to type your input text without worrying about manual line-breaks (of course, except when -- you want to leave intentional line-breaks to start a new line in your output or a manual line-break followed by a blank line to create a new paragraph in your output).

This macro can be defined as:

\newcommand{\hashed}[2]{#2}

OR as:

\newcommand{\hashed}[1]{\ignorespaces}

I would prefer the earlier than latter (for no specific reason whatsoever! It's just that I've been using the same for a while). Add either to your document preamble (before \begin{document}) and you can use it as:

The Red dragon was mighty \& strong. \hashed{His scales were stronger than any steel man has ever known!} His name was Shruikan.

Result for both these commands would look like this:

The Red dragon was mighty & strong.<space>His name was Shruikan.

As I have mentioned above in my comment on @cptnjtk's solution, if this macro was defined as suggested by @cptnjtk, i.e. --

\newcommand{\hashed}[1]{}

The above result would look like this:

The Red dragon was mighty & strong.<space><space>His name was Shruikan.

To avoid inserting such additional spaces, use either of the earlier mentioned macros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.