Well.
The IGI lists the births of George on 19 March 1838, Alexander on 24 March 1840, James on 3 August 1842, and John on 26 September 1844; the IGI dates correspond to those in the SP index, as indeed they should, as the IGI is an index to the births and baptisms in the OPRs which are on SP.
The 1851 census lists George, 13; Alexander, 11; James, 8; William, 3; and Jane, under 1 year. As the census was taken on 30 March 1851, this should mean that William was born between 31 March 1847 and 30 March 1848, and Jane on or after 31 March 1851.
The 1861 lists William, 12; Jean, 10; John, 8; and Jessie Ann, 5. The census date in 1861 was 7 April, which should mean that William was born between 8 April 1848 and 7 April 1849, Jane between 8 April 1850 and (from the 1851 census date) 30 March 1851, and John between 8 April 1852 and 7 April 1853. Jessie Ann was born on 5 May 1856. This suggests a gap of about 4 years between John in 1844 and William in 1848/1849, which is long enough for another child.
The fact that John is not in either census, and that a later son was named John, suggests that John b 1842 did indeed die in infancy. Perhaps Thomas and the William who died in infancy were twins born between John in 1844 and the William who survived?
The LIBINDX reference is indeed from the gravestone.
From previous attempts to find deaths in 1842 in Grange for someone, I am fairly sure that the Grange burial records for that period have not survived, but there is a chance that there might be something in the Grange Kirk Session records - see
https://catalogue.nrscotland.gov.uk/nrsonlinecatalogue/search.aspx and search for reference CH2/541