Number last line with listings package in LaTeX - latex

I'm using LaTeX with the listings package, and I number every fifth line of the code by having:
\lstset{
numbers=left,
stepnumber=5,
firstnumber=1,
numberfirstline=true
}
Now because of the numberfirstline=true in these settings, apart from the multiples of five the first line of each listing is numbered too, but I would like to have the last line of the code be numbered as well. So if I have a listing with 17 lines of code, the lines I would like to see numbered are 1 5 10 15 17.
Is there some way that I can make listings behave like this? I tried numberlastline=true, but that does not seem to exist.
Edit: I would prefer to use this with \lstinputlisting, and without having to modify the code that get imported that way.

Actually, since you're using numberfirstline=true, this is simpler to accomplish than you might think. The listings package provides a mechanism to escape out of the verbatim mode in the middle of a listing via escapeinside to be able to insert additional macros. The trick here is that after returning from the escaped code, it considers the next line as the "first" line again, even though no counts are reset. Thus, all you have to do is escape out (and do nothing!) on the second to last line of the listing. If you do it on the very last, you're too late! The example below demonstrates using this to get what you desire.
\documentclass{article}
\usepackage{listings}
\lstset{
numbers=left,
stepnumber=5,
firstnumber=1,
numberfirstline=true
escapeinside={|(}{)|}
}
\begin{document}
\begin{lstlisting}
lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore
eu fugiat nulla pariatur.
excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt|()|
mollit anim id est laborum.
\end{lstlisting}
\end{document}
You should also check out this question about arbitrarily altering line numbering.
UPDATE
This requires modification to the listing source, and as such may be insufficient when using \lstinputlisting. An alternative approach is to modify the logic of the listings package itself to number the last line when it is explicitly specified via the lastline or linerange keys, as is demonstrated below.
\documentclass{article}
\usepackage{listings}
\lstset{
numbers=left,
stepnumber=5,
firstnumber=1,
numberfirstline=true,
}
\makeatletter
\gdef\lst#SkipOrPrintLabel{%
\ifnum\lst#skipnumbers=\z#
\global\advance\lst#skipnumbers-\lst#stepnumber\relax
\lst#PlaceNumber
\lst#numberfirstlinefalse
\else
\ifnum\lst#lineno=\lst#lastline
\lst#PlaceNumber
\lst#numberfirstlinefalse
\fi
\lst#ifnumberfirstline
\lst#PlaceNumber
\lst#numberfirstlinefalse
\fi
\fi
\global\advance\lst#skipnumbers\#ne}
\makeatother
\begin{document}
\lstinputlisting[lastline=13]{input.tex}
\end{document}
This does require you to manually specify the last line number for each listing, but this seems like a reasonable sacrifice considering the difficulty of manipulating temporary files, which may potentially be the only real solution to dynamically discovering line count which only requires pure TeX. If a non-portable solution is acceptable, you could run a shell command such as wc -l via \write18 to get the number of lines. This would of course require the chosen command to be present on all systems the source is used on, which may cause difficulties if those systems span across different operating systems. See this question for an explanation of \write18 and this question for how to capture it's output.
Worth noting is that when using linerange, it will number the last line of each individual range, but not the first line for each range when using numberfirstline=true. While this can similarly be changed, linerange already results in enough such quirks that if this would be a problem, then it is most likely separate from the issue at hand here.

Related

Get full text of a direct message with 140+ characters with the Twitter API

Is it possible to get the full text of a Twitter direct message which is longer than 140 characters?
https://api.twitter.com/1.1/direct_messages/show.json?id=...
The field "text" of the response is truncated and doesn't show the full text.
"text": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et ... [LINK TO THE DM]"
I tried adding "tweet_mode=extended" to the URL, but that doesn't have any effect on direct messages.
Is there a different way to get the untruncated text?
Send “full_text=true” as a query parameter to receive long DM text. If
this parameter is not provided, you will get a truncated version of
the DM. There will be no structural changes to the response returned
by these endpoints.
Source: https://twittercommunity.com/t/removing-the-140-character-limit-from-direct-messages/41348

Twitter Button, share tweets

I hope, I can write tweet like this:
some text Lorem ipsum #myhastag Duis efficitur risus et augue tempus tristique.
so the format is : text #hashtag text.
so in html I wrote,
SHARE
But, the result just
Lorem ipsum #myhashtag
I have searched on https://dev.twitter.com/web/tweet-button and youtube video tutorial but still no idea.
I hope someone can help me, write tweet with format
text #hashtag text.
You need to Percent Encode the hash symbol as %23
Try this:
https://twitter.com/intent/tweet?text=Text%20%23hashtag%20more%20text

how to do pause after paragraph while textToSpeech

i have a text which is used for textToSpeech.
i want to do a pause in the text which is readed from the dragon mobile SDK for iPhone.
the text is without the SSML. only blanc text.
What kind of charactars can i use for pause? I know that after a " . " is a pause, i tried to do two . like ". ." but doesnt work.
How can i do pause after paragraph?
Example:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
//Do here a long pause from 3 seconds
Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
i have found it. no way out to use SSML. and SSML is incredible :)
have to use the break tag in SSML, see here: http://www.w3.org/TR/speech-synthesis/#S3.2.3

Extract IMG tags in Ruby

Is it possible to extract the IMG tag (or just the src attribute of an IMG tag) from a block of HTML in Ruby?
For example, if I have a block of HTML such as:
<p>Lorem ipsum dolor sit amet, labore et dolore magna aliqua.<img src="example.jpg" alt="" /> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
Could I extract just the IMG tag or src of that IMG tag via Regex or some other method?
Thanks in advance for any suggestions!
Using Nokogiri:
require 'nokogiri' # gem install nokogiri
doc = Nokogiri::HTML( my_html_string )
img_srcs = doc.css('img').map{ |i| i['src'] } # Array of strings
You can use this regular expression
html_str[/img.*?src="(.*?)"/i,1]
If you want a more advance html parser, I recommend nokogiri
Use Nokogiri to parse the HTML and search for img tags to extract the src attribute from.
There are many ways to do this. I prefer using the Nokogiri gem.
Before you get too far into this I suggest reading the following written by Jeff Atwood regarding parsing with Regex: Parsing Html The Cthulhu Way

ruby on rails regular expression find and remove tags between tags in html string

I'm working in ruby on rails and need the following:
remove all "br" html tags between "code" html tags in a string of html. The "code" tags might occur more than once.
Now, it's not screen scraping I'm trying to do. I have a blog and would like to allow people to use the code html tags only in the comments. So when formatting the string I normally use simple_format but I'd like it to ignore code html tags.
Thanks in advance.
If you absolutely positively have to use regexp, try this one, which catches all <br>, <br/> and <br /> tags:
str.gsub(/<code>.+?<\/code>/) {|s| s.gsub(/<br\s*\/?>/, "")}
Tested with:
str = "Lorem ipsum dolor sit amet<br />, <code>consectetur adipisicing elit<br />, sed do eiusmod tempor incididunt ut labore<br> et dolore magna aliqua</code>. Ut enim ad minim veniam,<br> quis nostrud exercitation ullamco laboris nisi<br/> ut aliquip ex ea commodo consequat. <code>Duis aute irure dolor in reprehenderit<br /> in voluptate velit esse cillum dolore<br/> eu fugiat nulla pariatur.</code> Excepteur sint occaecat cupidatat non proident,<br /> sunt in culpa qui officia deserunt mollit anim id est laborum."
p str.gsub(/<code>.+?<\/code>/) {|s| s.gsub(/<br\s*\/?>/, "")}
If you don't have to use regexp, use an html parser like nokogiri.
Using Hpricot or a HTML parser of your choice would be a far, far better idea.
I second on Hpricot, but what are trying to do? Attempting to do some sort of web-scraping or are you parsing the HTML from a model?

Resources