text: Pedro Sanchez has been trying to secure support for a coalition government with the centre-right Ciudadanos party following inconclusive December polls.
  In an often acrimonious debate, acting PM Mariano Rajoy said a Socialist-led coalition would be a threat to Spain's national interests.
  Another vote will be held on Friday.
  If that vote is also unsuccessful, parliament will have a further two months to choose a government. If it is unable to do, fresh elections will be held on 26 June.
  Blame game begins in earnest
  Strain of Sanchez's bid to rule
  Kiss that showed real political passion
  Mr Sanchez needed an absolute majority in Wednesday's confidence vote but lost, with 219 votes against, 130 in favour and one abstention in the 350-seat lower house.
  Mr Rajoy - leader of the incumbent conservative Popular Party (PP) - called Mr Sanchez a "fictitious, unreal candidate".
  He told Mr Sanchez the PP's 122 deputies would vote against him "because you plan to eliminate what was achieved in Spain throughout these past four years which prevented this country from needing a bailout, created jobs, improved its competitiveness and caused it to grow economically".
  Conversely, Mr Sanchez was also under attack from the left.
  "You want to consolidate the main policies of the PP," said Pablo Iglesias, the pony-tailed leader of the far-left Podemos party, which represents 69 seats.
  However, he did not rule out a united front with the Socialists entirely, urging Mr Sanchez to "write the future of Spain together with us" - but leaving aside Podemos's ideological foes, Ciudadanos.
  On Friday, Mr Sanchez will have another chance in a vote that requires only a simple majority.
  However, correspondents say that now looks doomed too - leaving the country in limbo at a time when the economy is growing but still suffers serious weaknesses, primarily an unemployment rate of nearly 21%.
  In an address to parliament on Tuesday, Mr Sanchez called for the formation of a coalition based on common interests.
  He said a Socialist-led government would enact a series of progressive measures such as a minimum wage increase and a gender wage-gap law.
  Between them, the Socialist PSOE and partner party Ciudadanos command only 130 seats in the lower chamber.
  The Popular Party gained most votes in the 20 December election but Mr Rajoy was unable to secure enough backing to form a government.
  The PSOE performed badly, hit by the emergence of Podemos and Ciudadanos, and the fragmented political landscape has eluded efforts to agree a governing coalition.
target: Spain's Socialist leader has lost a bid to form a government after both main rival parties voted down his attempts to form a coalition.